110,567
社区成员
发帖
与我相关
我的任务
分享
protected void btnCreate_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.Application excel = null;
Microsoft.Office.Interop.Word.Application word = null;
Microsoft.Office.Interop.Word.Document doc=null;
object SaveChanges = true; //保存更改
object OriginalFormat = System.Type.Missing;
object RouteDocument = System.Type.Missing;
try
{
string path = Server.MapPath("Doc/");//目录
string infoListXLS = path + "myexcel.xls";//EXCEL模版
string wordTemplate = path + "template.doc";//WORD模版
excel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workBook = excel.Workbooks.Open(infoListXLS, System.Type.Missing, false, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
//Microsoft.Office.Interop.Excel.Worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workBook.ActiveSheet;
excel.Visible = false;
int intRow = 2;
//string repStr = "&Chr(13)&Chr(10)";
//string desStr = "@";
//开始循环,直到第十一列“题干”为空时退出循环
while (excel.Cells[intRow, 11].ToString() != "")
{
//取得第十一列内容即 题干
string ti = excel.Cells[intRow, 11].ToString();
//取得第十二列内容即 内容,实现把@替换成Word中的换行符
string daan = excel.Cells[intRow, 12].ToString();
string docFile = path + ti + ".doc";
if (!File.Exists(docFile))
{
File.Copy(wordTemplate,docFile);
word = new Microsoft.Office.Interop.Word.Application();
word.Visible = false;
object fileName = docFile; //文件名称
object ConfirmConversions = false; //允许转换
object ReadOnly = false; //只读方式打开
object AddToRecentFiles = false; //添加到最近打开的文档
object PasswordDocument = System.Type.Missing;
object PasswordTemplate = System.Type.Missing;
object Revert = System.Type.Missing;
object WritePasswordDocument = System.Type.Missing;
object WritePasswordTemplate = System.Type.Missing;
object Format = System.Type.Missing; //格式
object Encoding = System.Type.Missing; //编码
object Visible = System.Type.Missing;
object OpenAndRepair = System.Type.Missing;
object DocumentDirection = System.Type.Missing;
object NoEncodingDialog = System.Type.Missing;
object XMLTransform = System.Type.Missing;
doc = word.Documents.Open(ref fileName, ref ConfirmConversions, ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate, ref Revert, ref WritePasswordDocument, ref WritePasswordTemplate, ref Format, ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection, ref NoEncodingDialog, ref XMLTransform);
Microsoft.Office.Interop.Word.Table docTable = doc.Tables[0];
docTable.Cell(1, 1).Range.Text = ti;
docTable.Cell(2, 1).Range.Text = daan;
doc.Save();
doc.Close(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
word.Quit(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
intRow++;
}
excel.Quit();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (doc != null)
{
doc.Close(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
}
if (word != null)
{
word.Quit(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
}
excel.Quit();
}
}
for ( j = 0; j < 26; j++) //xlsSheet.Cells.Rows.Count
{
for ( i = 0; i < 26; i++) //xlsSheet.Cells.Columns.Count
{
if (i == 8)
{
xlsSheet.get_Range(xlsSheet.Cells[j + 1, i + 1], xlsSheet.Cells[j + 1, i + 1]).NumberFormatLocal = "0";//"G/通用格式" @ General
xlsSheet.get_Range(xlsSheet.Cells[j + 1, i + 1], xlsSheet.Cells[j + 1, i + 1]).Interior.ColorIndex = 18;
}
str = str + "," + xlsSheet.Cells[i+1, j+1].ToString().Trim();
}
}