·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> c#(.net) 导出 word表格

c#(.net) 导出 word表格

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

c#(.net) 导出 Word表格

做了差不多一周的导出Word,现在把代码贴出来 :

ExportWord.cs

  1 using System;  2 using System.Collections.Generic;  3 using System.Linq;  4 using System.Web;  5 using System.Data;  6 using System.IO;  7   8   9 /// <summary> 10 ///DaoChuWord 的摘要说明 11 /// </summary> 12 public class ExportWord 13 { 14     public ExportWord() 15     { 16         // 17         //TODO: 在此处添加构造函数逻辑 18         // 19     } 20  21     public DataTable dat = null; 22     public void PRocessRequest(HttpContext context, string id,string name) 23     { 24         context.Response.ContentType = "text/plain"; 25         GetDataTable(id); 26         CreateWord(name);   27         //此处为了批量操作,把下面这个注掉 28         //context.Response.End(); 29  30     } 31  32     DataTable GetDataTable(string id) 33     { 34         string sql = " select * from hr_person where id =" + id; 35         dat = ZWL.DBUtility.MyDBHelp.GetDataTable(sql); 36         if (dat != null) 37         { 38             return dat; 39         } 40         throw new NoNullAllowedException(); 41     } 42  43     public string CreateWord(string name) 44     { 45         string uid = dat.Rows[0]["id"].ToString(); 46         string message = ""; 47          49             Object Nothing = System.Reflection.Missing.Value; 50             52             object filename = name;  //文件保存路径   53          54             //创建Word文档  55             Microsoft.Office.Interop.Word.application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass(); 56            57             Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing); 58             WordApp.Selection.PageSetup.LeftMargin = 50f; 59             WordApp.Selection.PageSetup.RightMargin = 50f; 60             WordApp.Selection.PageSetup.PageWidth = 650f;  //页面宽度  61  62             WordDoc.ActiveWindow.Selection.Font.Bold = 2; 63  64              70             WordApp.Selection.ParagraphFormat.LineSpacing = 13f;//设置文档的行间距       71             //移动焦点并换行  72             object count = 14; 73             object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdLine;//换一行; 74             WordApp.Selection.ParagraphFormat.LineSpacing = 18f; 75              76             WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; 77             WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点  78             WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; 79             WordApp.Selection.TypeParagraph();//插入段落  80             WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft; 81             WordApp.Selection.Font.Size = 18f; 82  83             object missing = System.Reflection.Missing.Value; 84             object count2 = 14; 85             object WdLine2 = Microsoft.Office.Interop.Word.WdUnits.wdLine;//换一行;    86             WordApp.Selection.Text = "应 聘 报 名 表"; 87             WordApp.Selection.ParagraphFormat.LineSpacingRule = Microsoft.Office.Interop.Word.WdLineSpacing.wdLineSpaceSingle;//单倍行距 88  89             90             WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; 91             WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点  92             WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; 93             WordApp.Selection.TypeParagraph();//插入段落  94              95             WordApp.Selection.Font.Size = 10.5f; 96             WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft; 97             WordApp.Selection.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorBlack; 98  99             //文档中创建表格 100             Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 19, 15, ref Nothing, ref Nothing);101             //设置表格样式 102             WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中 103 104             //设置表格框105             newTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;106             newTable.Columns[1].Width = 25f;107             newTable.Columns[2].Width = 30f;108             newTable.Columns[3].Width = 25f;109             newTable.Columns[4].Width = 45f;110             newTable.Columns[5].Width = 25f;111             newTable.Columns[6].Width = 25f;112             newTable.Columns[7].Width = 35f;113             newTable.Columns[8].Width = 40f;114             newTable.Columns[9].Width = 35f;115             newTable.Columns[10].Width = 15f;116             newTable.Columns[11].Width = 45f;117             newTable.Columns[12].Width = 30f;118             newTable.Columns[13].Width = 40f;119             newTable.Columns[14].Width = 50f;120             newTable.Columns[15].Width = 90f;121 122             //填充表格内容 123             for (int k = 1; k < 19; k++)124             {125                 newTable.Rows[k].Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;126             }127             for (int k = 1; k < 15; k++)128             {129                 newTable.Columns[k].Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;130             }131             //垂直居中132             object unit = Microsoft.Office.Interop.Word.WdUnits.wdLine;133             object countjz = 1;134             WordApp.Selection.MoveEnd(ref unit, ref countjz);135             WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中 136 137             //填充表格内容 第一行138             newTable.Cell(1, 1).Range.Text = "姓名";139           //合并单元格 140             newTable.Cell(1, 1).Merge(newTable.Cell(1, 2));141             newTable.Cell(1, 2).Range.Text = dat.Rows[0]["name"].ToString();142 143             newTable.Cell(1, 2).Merge(newTable.Cell(1, 3));144             newTable.Cell(1, 3).Range.Text = "性 别";145             newTable.Cell(1, 3).Merge(newTable.Cell(1, 4));146             newTable.Cell(1, 4).Range.Text = dat.Rows[0]["sex"].ToString();147             newTable.Cell(1, 4).Merge(newTable.Cell(1, 5));148             newTable.Cell(1, 5).Range.Text = "出  生日  期";149             newTable.Cell(1, 5).Merge(newTable.Cell(1, 6));150             if (dat.Rows[0]["createdate"].ToString().Equals(""))151             {152                 newTable.Cell(1, 6).Range.Text = "";153             }154             else155             {156                 newTable.Cell(1, 6).Range.Text = DateTime.Parse(dat.Rows[0]["createdate"].ToString()).ToString("yyyy-MM-dd");157             }158             newTable.Cell(1, 6).Merge(newTable.Cell(1, 7));159             newTable.Cell(1, 7).Range.Text = "民 族";160             newTable.Cell(1, 8).Range.Text = dat.Rows[0]["minzu"].ToString();161 162             newTable.Cell(1, 9).Merge(newTable.Cell(3, 15));163             newTable.Cell(1, 9).Select();//选中一行 164 165             string FileName = "E:\\OA\\new\\web2\\upload\\" + dat.Rows[0]["touxiang"].ToString();166             object LinkToFile = false;167             object SaveWithDocument = true;168             object Anchor = WordDoc.Application.Selection.Range;169             WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);170             WordDoc.Application.ActiveDocument.InlineShapes[1].Width = 72f;//图片宽度 171             WordDoc.Application.ActiveDocument.InlineShapes[1].Height = 90f;//图片高 172             WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;173 174             // 填充表格内容 第二行175             newTable.Cell(2, 1).Range.Text = "籍  贯";176             newTable.Cell(2, 1).Merge(n