winfrom中怎么样导出word文档 我有一些字符串..本来可以到处Excel的但是现在要到出word文档..求高手帮下忙..最好能贴点代码出来......... 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 /// <summary> /// 导出Excel /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button1_Click(object sender, EventArgs e) { Export("application/ms-excel", "Employee information.xls"); } /// <summary> /// 定义导出Excel的函数 /// </summary> /// <param name="FileType"></param> /// <param name="FileName"></param> private void Export(string FileType, string FileName) { Response.Charset = "GB2312"; Response.ContentEncoding = System.Text.Encoding.UTF8; Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString()); Response.ContentType = FileType; this.EnableViewState = false; StringWriter tw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(tw); GridView1.RenderControl(hw); Response.Write(tw.ToString()); Response.End(); } /// <summary> /// 此方法必重写,否则会出错 /// </summary> /// <param name="control"></param> public override void VerifyRenderingInServerForm(Control control) { } protected void Button2_Click(object sender, EventArgs e) { //Export("application/ms-excel", "Employee.doc"); Export("application/ms-word", "员工信息.doc");//都可以 } protected void outport_Click(object sender, EventArgs e) { Export("application/ms-excel", "xxx.dox"); } private void Export(string FileType, string FileName) { Response.Charset = "GB2312"; Response.ContentEncoding = System.Text.Encoding.UTF7; Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString()); Response.ContentType = FileType; this.EnableViewState = false; StringWriter tw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(tw); GV_cg.RenderControl(hw); //DataList1.RenderControl(hw); Response.Write(tw.ToString()); Response.End(); } public override void VerifyRenderingInServerForm(Control control) { } 我要导出word文档...winform应用程序啊.... 也可以使用Office组件,自己写代码也可以。 public static void ExportData(DataGridView srcDgv,string fileName)//导出数据,传入一个datagridview和一个文件路径{string type = fileName.Substring(fileName.IndexOf(”.”)+1);//获得数据类型if (type.Equals(”xls”,StringComparison.CurrentCultureIgnoreCase))//Excel文档{Excel.Application excel = new Excel.Application();try{excel.DisplayAlerts = false;excel.Workbooks.Add(true);excel.Visible = false;for (int i = 0; i < srcDgv.Columns.Count; i++)//设置标题{excel.Cells[2, i+1] = srcDgv.Columns[i].HeaderText;}for (int i = 0; i < srcDgv.Rows.Count; i++)//填充数据{for (int j = 0; j < srcDgv.Columns.Count; j++){excel.Cells[i + 3, j + 1] = srcDgv[j, i].Value;}}excel.Workbooks[1].SaveCopyAs(fileName);//保存}finally{excel.Quit();}return;}if (type.Equals(”doc”, StringComparison.CurrentCultureIgnoreCase)){object path = fileName;Object none=System.Reflection.Missing.Value;Word.Application wordApp = new Word.Application();Word.Document document = wordApp.Documents.Add(ref none, ref none, ref none, ref none);Word.Table table= document.Tables.Add(document.Paragraphs.Last.Range, srcDgv.Rows.Count+1, srcDgv.Columns.Count, ref none, ref none);try{for (int i = 0; i < srcDgv.Columns.Count; i++)//设置标题{table.Cell(1, i + 1).Range.Text = srcDgv.Columns[i].HeaderText;}for (int i = 0; i < srcDgv.Rows.Count; i++)//填充数据{for (int j = 0; j < srcDgv.Columns.Count; j++){table.Cell(i + 2, j + 1).Range.Text = srcDgv[j, i].Value.ToString();}}document.SaveAs(ref path, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none);document.Close(ref none, ref none, ref none);}finally{wordApp.Quit(ref none, ref none, ref none);}}}WORD 操作 谢谢 [ wuyq11 的回复:]我找了段替换的代码,导出了固定的格式word.. 现在又来了个问题. 我有张图片该怎么样插入word文档里面了... 流媒体,H.264,RTP,FU-A,解码播放 软件拿到国外去用,应该如何修改? xml序列化如何解决递归对象的嵌套问题呀? 从Listbox中取值的问题 想学习.net从哪入手,不知从哪开始 RichTextBox控件输入的文本怎么才能不与前面格式一样啊? 关于DataList的一个问题 高分求助 怎样判断一个窗口是否已经打开? 如何将string转化为int 请各位帮助?请指出“异常”发生的确切位置? winform 水晶报表 子报表绑定dataset 高手救命
{
Export("application/ms-excel", "xxx.dox");
}
private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GV_cg.RenderControl(hw);
//DataList1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
{
string type = fileName.Substring(fileName.IndexOf(”.”)+1);//获得数据类型
if (type.Equals(”xls”,StringComparison.CurrentCultureIgnoreCase))//Excel文档
{
Excel.Application excel = new Excel.Application();
try
{
excel.DisplayAlerts = false;
excel.Workbooks.Add(true);
excel.Visible = false;
for (int i = 0; i < srcDgv.Columns.Count; i++)//设置标题
{
excel.Cells[2, i+1] = srcDgv.Columns[i].HeaderText;
}for (int i = 0; i < srcDgv.Rows.Count; i++)//填充数据
{
for (int j = 0; j < srcDgv.Columns.Count; j++)
{
excel.Cells[i + 3, j + 1] = srcDgv[j, i].Value;
}
}
excel.Workbooks[1].SaveCopyAs(fileName);//保存
}
finally
{
excel.Quit();
}
return;
}
if (type.Equals(”doc”, StringComparison.CurrentCultureIgnoreCase))
{
object path = fileName;
Object none=System.Reflection.Missing.Value;
Word.Application wordApp = new Word.Application();
Word.Document document = wordApp.Documents.Add(ref none, ref none, ref none, ref none);
Word.Table table= document.Tables.Add(document.Paragraphs.Last.Range, srcDgv.Rows.Count+1, srcDgv.Columns.Count, ref none, ref none);
try
{
for (int i = 0; i < srcDgv.Columns.Count; i++)//设置标题
{
table.Cell(1, i + 1).Range.Text = srcDgv.Columns[i].HeaderText;
}
for (int i = 0; i < srcDgv.Rows.Count; i++)//填充数据
{
for (int j = 0; j < srcDgv.Columns.Count; j++)
{
table.Cell(i + 2, j + 1).Range.Text = srcDgv[j, i].Value.ToString();
}
}
document.SaveAs(ref path, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none);
document.Close(ref none, ref none, ref none);
}
finally
{
wordApp.Quit(ref none, ref none, ref none);
}
}
}
WORD 操作
[ wuyq11 的回复:]我找了段替换的代码,导出了固定的格式word..
现在又来了个问题. 我有张图片该怎么样插入word文档里面了...