楼上的 你能不能改写下这个例子啊? 我这个只能导出字符 不能导出表格
string title = "个人信息";
object titleLengh = title.Length;
string first = "\n 公司最近需要利用C#对项目进行编程,其" +
"中存在一个功能就是可自动生成WORD文档,但一直以来都" +
"找不到什么好办法,无奈之下,只有自已学着写一个了.";
object firstLengh = first.Length;
string second = "\n 如果能真正生成WORD文档的好处有:";
object secondLengh = second.Length;
string third = "\n1、根据数据库信息自动生成文档;";
object thirdLengh = third.Length;
string forth = "\n2、免去书写文档之苦;";
object forthLengh = forth.Length;
string fifth = "\n3、可以通过邮件方式传出文档。";
object fifthLengh = fifth.Length;
object missing;
object zero = 0;
object c = 10;
object one = 1;
object two = 2;
object tree = 3;
object four = 4;
object five = 5;
object six = 6;
object seven = 7;
object eight = 8;
object nine = 9;
object ten = 10;
Object Nothing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.ApplicationClass wa = new Microsoft.Office.Interop.Word.ApplicationClass();
Microsoft.Office.Interop.Word.Document WordDoc = wa.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
missing = System.Reflection.Missing.Value;
wa.Visible = true;
wa.Documents.Add(ref missing, ref missing, ref missing, ref missing);
Microsoft.Office.Interop.Word.Range myRange = wa.ActiveDocument.Range(ref zero, ref zero);
object r = myRange;
Microsoft.Office.Interop.Word.Paragraph p = wa.ActiveDocument.Paragraphs.Add(ref r);
p.Range.InsertBefore(title);
//p.Range.Font.Size = 1;
Microsoft.Office.Interop.Word.Range titleRange = wa.ActiveDocument.Range(ref zero, ref titleLengh);
//titleRange.Font.Size = 1;
titleRange.Font.Name = "幼圆";
titleRange.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorBlue;
//MessageBox.Show("NO.1");
//titleRange.Paragraphs.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
Microsoft.Office.Interop.Word.Range firstR = wa.ActiveDocument.Paragraphs[1].Range;//.Item(2).Range;
Microsoft.Office.Interop.Word.Table table = WordDoc.Tables.Add(titleRange, 3, 3, ref Nothing, ref Nothing);
r = firstR;
p = wa.ActiveDocument.Paragraphs.Add(ref r);
firstR.Font.Size = 40;
firstR.Paragraphs.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
//在表格第一单元格中添加自定义的文字内容
table.Cell(1, 1).Range.Text = "lllll";
firstR.InsertAfter(first);
//firstR.InsertParagraphAfter();
//firstR=wa.ActiveDocument.Paragraphs.Item(3).Range;
firstR.InsertAfter(second);
firstR.InsertAfter(third);
firstR.InsertAfter(forth);
firstR.InsertAfter(fifth);
Context.Response.Write("成功");
public static void ExportWord(Control control, HttpResponse response, string fileName)
{
string strName = HttpUtility.UrlEncode(fileName);
response.Clear();
response.Buffer = true;
response.Charset = "utf-7";
response.AppendHeader("Content-Disposition", "attachment;filename=" + strName + ".doc");
response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-7");//设置输出流为简体中文
response.ContentType = "application/ms-word";//设置输出文件类型为Word文件。
response.AppendHeader("xml", @"
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
if (control != null)
{
control.RenderControl(oHtmlTextWriter);//将服务器控件的内容输出
}
response.Write(oStringWriter.ToString());
response.End();
}