动态创建word后下载(一)
思路:添加office引用--->实例化后设置word 格式(页眉页脚、行列、样式)---->保存到目录文件---->文件流的方式输出进行下载
第一:在bin里添加引用,命名空间:using Microsoft.Office.Interop.Word;
第二:实例化对象设置格式
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 Object Nothing = System.Reflection.Missing.Value; 2 3 //Directory.CreateDirectory("D:/ST"); //创建文件所在目录 4 //string name = "ST" + DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + ".doc"; 5 //object filename = "D://ST//" + name; //文件保存路径 6 7 //或者指向远程服务器共享目录 8 object filename = FileHelper.GetUploadPath(FileHelper.TestDownFile) + DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + ".doc"; 9 10 //创建Word文档 11 Application WordApp = new ApplicationClass(); 12 13 Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing); 14 //添加页眉 15 WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView; 16 WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader; 17 WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("[页眉内容]"); 18 WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight;//设置右对齐 19 WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;//跳出页眉设置 20 21 WordApp.Selection.ParagraphFormat.LineSpacing = 15f;//设置文档的行间距 22 23 //移动焦点并换行 24 object count = 14; 25 object WdLine = WdUnits.wdLine;//换一行; 26 WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点 27 WordApp.Selection.TypeParagraph();//插入段落 28 29 //文档中创建表格 (5,2 即:五行两列) 30 Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 5, 2, ref Nothing, ref Nothing); 31 //设置表格样式 32 newTable.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleThickThinLargeGap; 33 newTable.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle; 34 newTable.Columns[1].Width = 100f; 35 newTable.Columns[2].Width = 320f; 36 37 //填充表格内容 38 newTable.Cell(1, 1).Range.Text = "试题详细内容"; 39 newTable.Cell(1, 1).Range.Bold = 2;//设置单元格中字体为粗体 40 //合并单元格 41 newTable.Cell(1, 1).Merge(newTable.Cell(1, 2)); 42 WordApp.Selection.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中 43 WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;//水平居中 44 45 //填充表格内容 46 newTable.Cell(2, 1).Range.Text = "试题题干:"; 47 newTable.Cell(2, 2).Range.Text = TestContent; //StringHelper.HtmlSpecialEntitiesDecode(dtQuestion.Rows[0]["questionContent"].ToString());(从DB中查询或者传递过来的参数) 48 49 newTable.Cell(3, 1).Range.Text = "试题答案:"; 50 newTable.Cell(3, 2).Range.Text = TestAnswer; //dtQuestion.Rows[0]["objectiveAnswer"].ToString() + StringHelper.HtmlSpecialEntitiesDecode(dtQuestion.Rows[0]["subjectiveAnswer"].ToString()); 51 52 newTable.Cell(4, 1).Range.Text = "试题解析:"; 53 newTable.Cell(4, 2).Range.Text = analysis;// StringHelper.HtmlSpecialEntitiesDecode(dtQuestion.Rows[0]["analysis"].ToString()); 54 55 newTable.Cell(5, 1).Range.Text = "试题类型:"; 56 newTable.Cell(5, 2).Range.Text = dtQuestion.Rows[0]["questionTypeName"].ToString(); 57 //在表格中增加行 58 WordDoc.Content.Tables[1].Rows.Add(ref Nothing); //新增一空行 59 60 WordDoc.Paragraphs.Last.Range.Text = "文档创建时间:" + DateTime.Now.ToString();//(落款时间) 61 WordDoc.Paragraphs.Last.Alignment = WdParagraphAlignment.wdAlignParagraphRight; 62 63 //文件保存 (filename:指定存放目录) 64 WordDoc.SaveAs(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing); 65 WordDoc.Close(ref Nothing, ref Nothing, ref Nothing); 66 WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
第三:下载文件流的方式
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 string path = filename.ToString(); 2 3 string strName = path; 4 FileStream fileStream = new FileStream(strName, System.IO.FileMode.Open, 5 System.IO.FileAccess.Read, System.IO.FileShare.Read); 6 7 long fileSize = fileStream.Length; 8 string FileName = path.Split('/')[path.Split('/').Length - 1]; 9 byte[] fileBuffer = new byte[fileSize]; 10 fileStream.Read(fileBuffer, 0, (int)fileSize); 11 fileStream.Close(); 12 System.Web.HttpContext.Current.Response.Charset = "utf-8"; 13 14 if (Request.UserAgent.ToLower().IndexOf("msie") > -1) 15 { 16 FileName = ToHexString(FileName); 17 } 18 if (Request.UserAgent.ToLower().IndexOf("firefox") > -1) 19 { 20 System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=\"" + FileName + "\""); 21 } 22 else 23 { 24 System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName); 25 } 26 27 System.Web.HttpContext.Current.Response.BufferOutput = true; 28 System.Web.HttpContext.Current.Response.BinaryWrite(fileBuffer);
总结:找到vs提供的接口,进行实例化设置即可。思路很重要。