word 导出(带有一个图片,一张datatable)

View Code
 1  /// <summary>
 2         /// 不依赖Com组件利用第三方开源控件(DocX)导出标准Word文件,仅支持2007格式
 3         /// !以下导出仅适用于特定的格式(一个图片和一个DataTable),如要用于其他导出,请自行修改
 4         /// </summary>
 5         /// <param name="dt">要导出的DataTable</param>
 6         /// <param name="imagePath">要导出的图片路径</param>
 7         /// <param name="fileName">要导出的文件名称</param>
 8         public void ExportWord(DataTable dt, string imagePath, string fileName)
 9         {
10             //新建一个document文档
11             Novacode.DocX document = Novacode.DocX.Create(HttpContext.Current.Server.MapPath("Blank07.docx"));
12 
13             System.IO.MemoryStream file = new System.IO.MemoryStream();
14             //在这里写入word文件内容
15 
16             //插入统计结果图片
17             Novacode.Image image = document.AddImage(imagePath);
18             Novacode.Picture picture = image.CreatePicture();
19 
20             document.InsertParagraph().AppendPicture(picture);
21 
22             //插入统计结果表格数据,并设置单元格样式
23             Novacode.Table table = document.InsertTable(dt.Rows.Count, dt.Columns.Count);
24             //边框样式
25             Novacode.Border border = new Novacode.Border(Novacode.BorderStyle.Tcbs_single, Novacode.BorderSize.two, 0, System.Drawing.Color.Black);
26             //table.SetBorder(Novacode.TableBorderType.Bottom,Novacode.Border
27             for (int i = 0; i < dt.Rows.Count; i++)
28             {
29                 for (int j = 0; j < dt.Columns.Count; j++)
30                 {
31                     table.Rows[i].Cells[j].Paragraphs[0].Append(dt.Rows[i][j].ToString());
32                     table.Rows[i].Cells[j].SetBorder(Novacode.TableCellBorderType.Top, border);
33                     table.Rows[i].Cells[j].SetBorder(Novacode.TableCellBorderType.Right, border);
34                     table.Rows[i].Cells[j].SetBorder(Novacode.TableCellBorderType.Bottom, border);
35                     table.Rows[i].Cells[j].SetBorder(Novacode.TableCellBorderType.Left, border);
36                 }
37             }
38 
39             //插入统计结果表格标题,并设置单元格样式
40             Novacode.Row tableHeader = table.InsertRow(0);
41             for (int i = 0; i < dt.Columns.Count; i++)
42             {
43                 tableHeader.Cells[i].Paragraphs[0].Append(dt.Columns[i].ColumnName);
44                 tableHeader.Cells[i].SetBorder(Novacode.TableCellBorderType.Top, border);
45                 tableHeader.Cells[i].SetBorder(Novacode.TableCellBorderType.Right, border);
46                 tableHeader.Cells[i].SetBorder(Novacode.TableCellBorderType.Bottom, border);
47                 tableHeader.Cells[i].SetBorder(Novacode.TableCellBorderType.Left, border);
48             }
49 
50             document.SaveAs(file);
51             file.Close();
52             response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
53             string filename = HttpUtility.UrlEncode(fileName + ".docx");//url编码.防止中文乱码.
54             response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
55             response.BinaryWrite(file.GetBuffer());
56             response.Flush();
57             response.End();
58         }

说明:自己只是收集,以便以后自己的使用!希望对你也有所帮助!

posted @ 2012-07-03 09:07  Ruicky  阅读(577)  评论(0编辑  收藏  举报