国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設(shè)計 > 正文

c#(.net) 導出 word表格

2019-11-17 01:47:55
字體:
供稿:網(wǎng)友

c#(.net) 導出 Word表格

做了差不多一周的導出Word,現(xiàn)在把代碼貼出來 :

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: 在此處添加構(gòu)造函數(shù)邏輯 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             //創(chuàng)建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;//設(shè)置文檔的行間距       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             //文檔中創(chuàng)建表格 100             Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 19, 15, ref Nothing, ref Nothing);101             //設(shè)置表格樣式 102             WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中 103 104             //設(shè)置表格框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             //填充表格內(nèi)容 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             //填充表格內(nèi)容 第一行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             // 填充表格內(nèi)容 第二行175             newTable.Cell(2, 1).Range.Text = "籍  貫";176             newTable.Cell(2, 1).Merge(n
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 陕西省| 永兴县| 古蔺县| 康定县| 台南市| 南郑县| 温泉县| 乌兰县| 渝北区| 林甸县| 东安县| 桃江县| 雷山县| 商洛市| 尼玛县| 安多县| 常山县| 逊克县| 湾仔区| 岫岩| 莱西市| 寿光市| 陵川县| 沙洋县| 双鸭山市| 大同市| 马尔康县| 宁晋县| 营山县| 乌什县| 灵寿县| 景东| 巴林右旗| 武陟县| 阜城县| 万盛区| 华蓥市| 水城县| 水城县| 宜州市| 喜德县|