c#导出word文档总汇,转载
View Code
1 /// <summary> 2 /// 导出Word 3 /// </summary> 4 /// <param name="strContent">类容</param> 5 /// <param name="Col">列数</param> 6 public static void OutPutWordDT(List<string> strContent, int Col) 7 { 8 Object Nothing = System.Reflection.Missing.Value; 9 Application oword = new Application(); 10 Document odoc = oword.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing); 11 12 try 13 { 14 int Rows = strContent.Count % Col; 15 if (Rows != 0) 16 Rows = Rows + 1; 17 else 18 Rows = strContent.Count / Col; 19 20 odoc.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter; 21 22 Table otable = odoc.Tables.Add(oword.Selection.Range, Rows, Col, ref Nothing, ref Nothing); 23 otable.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;//设置对其方式 24 otable.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;//设置表格边框样式 25 26 //otable.Cell(1, 1).Merge(otable.Cell(2, 1)); //合并单元格 27 28 int index = 0; 29 30 for (int i = 1; i <= Rows; i++) 31 { 32 for (int j = 1; j <= Col && index < strContent.Count; j++, index++) 33 { 34 otable.Cell(i, j).Range.Text = strContent[index]; 35 otable.Cell(i, j).Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;//设置单元格样式 36 } 37 } 38 oword.Visible = true; 39 } 40 catch (Exception) { } 41 }
View Code
1 /// <summary> 2 /// 导出Word 3 /// </summary> 4 /// <param name="dt">导出的数据DataTable</param> 5 /// <param name="isColname">是否显示列名</param> 6 public static void OutPutWordDT(DataTable dt, bool isColname) 7 { 8 Object Nothing = System.Reflection.Missing.Value; 9 Application oword = new Application(); 10 Document odoc = oword.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing); 11 odoc.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter; 12 13 try 14 { 15 Table otable = odoc.Tables.Add(oword.Selection.Range, dt.Rows.Count + 1, dt.Columns.Count, ref Nothing, ref Nothing); 16 otable.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;//设置对其方式 17 otable.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;//设置表格边框样式 18 19 if (isColname) 20 { 21 int intcol = 0; 22 for (int ii = 0; ii < dt.Columns.Count; ii++) 23 { 24 intcol += 1; 25 otable.Cell(1, intcol).Range.Text = dt.Columns[ii].ColumnName; 26 otable.Cell(1, intcol).Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;//设置单元格样式 27 } 28 } 29 30 int intRow = 1; 31 for (int ii = 0; ii < dt.Rows.Count; ii++) 32 { 33 intRow += 1; 34 int intCol = 0; 35 for (int jj = 0; jj < dt.Columns.Count; jj++) 36 { 37 intCol += 1; 38 otable.Cell(intRow, intCol).Range.Text = dt.Rows[ii][jj].ToString(); 39 otable.Cell(intRow, intCol).Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;//设置单元格样式 40 } 41 } 42 oword.Visible = true; 43 } 44 catch (Exception) { } 45 }
View Code
1 /// <summary> 2 /// 导出Word 3 /// </summary> 4 /// <param name="strTitle">标题</param> 5 /// <param name="dt">导出的数据DataTable</param> 6 /// <param name="isColname">是否显示列名</param> 7 public static void OutPutWordDT(string strTitle, DataTable dt, bool isColname) 8 { 9 Object Nothing = System.Reflection.Missing.Value; 10 Application oword = new Application(); 11 Document odoc = oword.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing); 12 odoc.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter; 13 14 try 15 { 16 Table otable = odoc.Tables.Add(oword.Selection.Range, dt.Rows.Count + 2, dt.Columns.Count, ref Nothing, ref Nothing); 17 otable.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;//设置对其方式 18 otable.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;//设置表格边框样式 19 20 otable.Cell(1, 1).Merge(otable.Cell(1, dt.Columns.Count)); //合并单元格 21 otable.Cell(1, 1).Range.Text = strTitle; 22 otable.Cell(1, 1).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter; 23 otable.Cell(1, 1).Range.Font.Bold = 1; 24 otable.Cell(1, 1).Range.Font.Size = 20; 25 26 if (isColname) 27 { 28 int intCol = 0; 29 for (int ii = 0; ii < dt.Columns.Count; ii++) 30 { 31 intCol += 1; 32 otable.Cell(2, intCol).Range.Text = dt.Columns[ii].ColumnName; 33 otable.Cell(2, intCol).Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;//设置单元格样式 34 } 35 } 36 37 int intRow = 2; 38 for (int ii = 0; ii < dt.Rows.Count; ii++) 39 { 40 intRow += 1; 41 int intcol = 0; 42 for (int jj = 0; jj < dt.Columns.Count; jj++) 43 { 44 intcol += 1; 45 otable.Cell(intRow, intcol).Range.Text = dt.Rows[ii][jj].ToString(); 46 otable.Cell(intRow, intcol).Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;//设置单元格样式 47 } 48 } 49 oword.Visible = true; 50 } 51 catch (Exception) { } 52 }
/// <summary> /// 导出为Word /// </summary> /// <param name="dt">导出的数据</param> /// <param name="Col">多少列</param> /// <param name="strTitle">标题</param> public static void OutPutWordDT(DataTable dt, int Col, string strTitle) { Object Nothing = System.Reflection.Missing.Value; Application oword = new Application(); Document odoc = oword.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing); odoc.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter; try { int Rowint = (dt.Columns.Count * 2) % Col; if (Rowint != 0) Rowint = Rowint + 1; else Rowint = (dt.Columns.Count * 2) / Col; Table otable = odoc.Tables.Add(oword.Selection.Range, Rowint + 1, Col, ref Nothing, ref Nothing); otable.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;//设置对其方式 otable.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;//设置表格边框样式 otable.Cell(1, 1).Merge(otable.Cell(1, Col)); //合并单元格 otable.Cell(1, 1).Range.Text = strTitle; otable.Cell(1, 1).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter; otable.Cell(1, 1).Range.Font.Bold = 1; otable.Cell(1, 1).Range.Font.Size = 20; int icol = 0; for (int iRow = 2; iRow <= Rowint + 1; iRow++) { for (int incol = 1; incol <= Col && icol < dt.Columns.Count; incol++) { if (incol % 2 != 0) { otable.Cell(iRow, incol).Range.Text = dt.Columns[icol].ColumnName + ":"; otable.Cell(iRow, incol).Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;//设置单元格样式 } else { otable.Cell(iRow, incol).Range.Text = dt.Rows[0][icol].ToString(); otable.Cell(iRow, incol).Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;//设置单元格样式 icol++; } } } oword.Visible = true; } catch (Exception) { } }
private void Button13_Click(object sender, System.EventArgs e) { this.Datagrid4.Visible=true; Response.Clear(); Response.Buffer= true; Response.Charset="GB2312"; Response.AppendHeader("Content-Disposition","attachment;filename=File1.DOC"); Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312"); Response.ContentType = "application/ms-word"; this.Datagrid4.EnableViewState = false; System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter (oStringWriter); this.Datagrid4.RenderControl(oHtmlTextWriter); Response.Write(oStringWriter.ToString()); Response.End(); }
在做ASP.NET项目时,会经常遇到要导出文件的问题,如将DataGrid中的数据导出到excel文件等,经常使用的是Office中的OWC组件,这个组件提供的功能很强大,在一般的项目中都可以满足当前的需要.但是这个功能强大的组件使用起来却不是很方便,不但有版本的问题,而且代码量也相对比较大.如果要利用Respone对象和相关的IO,也可以实现到处excel/word等文件,而且使用方便.
代码如下:
View Code
System.IO.StringWriter SW = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter HTW=new System.Web.UI.HtmlTextWriter(SW); Page.RenderControl(HTW); //Page为要导出的对象,当前是Page,如果是DataGrid,DataList等都可以 Response.Buffer=true; Response.Clear(); Response.ClearContent(); Response.ClearHeaders(); Response.ContentType = "Response.ContentType"; //Response.ContentType是输出流的 HTTP MIME 类型 //Response.ContentType --- word文件 //application/vnd.ms-excel --- excel文件 // Response.Charset="utf-8"; Response.ContentEncoding=System.Text.Encoding.GetEncoding("utf-8"); Response.AddHeader("Content-Disposition", "attachment;filename=XXX.doc"); //attachment --- 作为附件下载 //inline --- 在线打开 //filename如过是中文,则可以用HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8) //进行进行编码,以解决文件名乱码的问题 Response.Write(SW.ToString()); Response.Flush(); Response.Close();
这样即可以将当前的页面导出成一个Word文件.同样原理要将DataGrid,DataList等中的内容导出为Word或Excel等类型的文件,那么只需要稍做修改代码即可.
OK了,这样比用OWC组件方便多了,但也有个小问题,那就是导出的文件从文件格式上来说不是"真正"Word或Excel类型的文件,但用Office照样能打开,没什么区别,用户是看不出来的,也不会去理会这样的事情~~~
再来一个
View Code
protected void Button_Click(object sender, EventArgs e) { System.IO.StringWriter SW = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter HTW = new System.Web.UI.HtmlTextWriter(SW); Page.EnableViewState = false; Page.Form.RenderControl(HTW); //Page为要导出的对象,当前是Page,如果是DataGrid,DataList等都可以 string pageHtml = SW.ToString(); int startIndex = pageHtml.IndexOf("<div id=/"PrintA/">"); int endIndex = pageHtml.LastIndexOf("</div>"); int lenth = endIndex - startIndex; pageHtml = pageHtml.Substring(startIndex, lenth); pageHtml = pageHtml.Remove(pageHtml.LastIndexOf("</div>")); //移除页面自动添加的hidden input等 Uri MyUrl = Request.UrlReferrer; pageHtml = pageHtml.Replace("../images/title.jpg", "http://" + MyUrl.Authority + "/" + MyUrl.AbsolutePath.Split('/').GetValue(1) + "/images/title.jpg").Replace("textarea", "span"); //解决图片无法显示问题 Response.Buffer = true; Response.Clear(); Response.ClearContent(); Response.ClearHeaders(); //Response.ContentType = "Response.ContentType"; Response.ContentType = "application/ms-word"; //Response.ContentType是输出流的 HTTP MIME 类型 //Response.ContentType --- word文件 //application/vnd.ms-excel --- excel文件 //... Response.Charset = "utf-8"; Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8"); Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("文档", System.Text.Encoding.UTF8) + ".doc"); //attachment --- 作为附件下载 //inline --- 在线打开 //filename如过是中文,则可以用HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8) //进行进行编码,以解决文件名乱码的问题 Response.Write(pageHtml.ToString()); Response.Flush(); Response.Close(); }

浙公网安备 33010602011771号