博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
网页中的数据导出问题(word文档等格式)
阅读量:4474 次
发布时间:2019-06-08

本文共 2761 字,大约阅读时间需要 9 分钟。

  方法一:
 
HttpContext.Current.Response.AppendHeader(
"
Content-Disposition
"
"
attachment;filename=
"
 
+
 filename);
HttpContext.Current.Response.Charset 
=
 
"
UTF-8
"
;
HttpContext.Current.Response.ContentEncoding 
=
 System.Text.Encoding.Default;
  HttpContext.Current.Response.ContentType 
=
 filetype;
 
//
HttpContext.Current.Response.ContentType = "application/ms-excel";
//
image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
HttpContext.Current.Response.Buffer 
=
 
true
System.IO.StringWriter tw 
=
 
new
 System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw 
=
 
new
 System.Web.UI.HtmlTextWriter(tw);
gv.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
  方法二:
 
private
   
void
   ExportWord_Click(
object
   sender,   System.EventArgs   e)  
  
{  
  
try  
  
{  
  
string   savepath   =   "";  
  FolderBrowserDialog   folderBrowserDialog1   
=   new   FolderBrowserDialog();  
  
if   (folderBrowserDialog1.ShowDialog()   ==   DialogResult.OK)    
  
{  
  savepath   
=   folderBrowserDialog1.SelectedPath;  
  }
  
   
  Object   Nothing
=System.Reflection.Missing.Value;  
  
object   filename   =   savepath   +   "/"   +   StoreNum.Text   +   "库存报表";  
  Word.Application   WordApp
=new   Word.ApplicationClass();  
  Word.Document   WordDoc
=WordApp.Documents.Add(ref   Nothing,ref   Nothing,ref   Nothing,ref   Nothing);  
   
  WordDoc.Paragraphs.First.Range.Text   
=   "库存报表       [共有:"   +   storedt.Rows.Count.ToString()   +   "本书]";  
  WordDoc.Paragraphs.First.Alignment   
=   Word.WdParagraphAlignment.wdAlignParagraphCenter;  
   
  Word.Table   table
=WordDoc.Tables.Add(WordApp.Selection.Range,storedt.Rows.Count+1,6,ref   Nothing,ref   Nothing);  
  table.Cell(
1,1).Range.Text="ISBN号";  
  table.Cell(
1,2).Range.Text="书名";  
  table.Cell(
1,3).Range.Text="总库存";  
  table.Cell(
1,4).Range.Text="借书库存";  
  table.Cell(
1,5).Range.Text="可借数量";  
  table.Cell(
1,6).Range.Text="已借数量";  
   
  
for   (   int   i=0;i<storedt.Rows.Count;i++   )  
  
{  
  table.Cell(i
+2,1).Range.Text   =   storedt.Rows[i]["Book_ISBN"].ToString();  
  table.Cell(i
+2,2).Range.Text   =   storedt.Rows[i]["Book_Name"].ToString();  
  table.Cell(i
+2,3).Range.Text   =   storedt.Rows[i]["Store_Num"].ToString();  
  table.Cell(i
+2,4).Range.Text   =   storedt.Rows[i]["CanBorrow_Num"].ToString();  
  table.Cell(i
+2,5).Range.Text   =   storedt.Rows[i]["InShop_Num"].ToString();  
  table.Cell(i
+2,6).Range.Text   =   storedt.Rows[i]["OutShop_Num"].ToString();  
  }
  
   
  WordDoc.SaveAs(
ref   filename,ref   Nothing,ref   Nothing,ref   Nothing,ref   Nothing,ref   Nothing,ref   Nothing,ref   Nothing,ref   Nothing,ref   Nothing,ref   Nothing,ref   Nothing,ref   Nothing,ref   Nothing,ref   Nothing,ref   Nothing);  
  WordDoc.Close(
ref   Nothing,   ref   Nothing,   ref   Nothing);  
  WordApp.Quit(
ref   Nothing,   ref   Nothing,   ref   Nothing);  
  parent.statusBar1.Panels[
0].Text   =   "操作提示:库存报表导出成功";  
  }
  
  
catch  
  
{  
  parent.statusBar1.Panels[
0].Text   =   "操作提示:库存报表导出失败";  
  }
  
  }
  
   
  注意要引用Interop.Word.dll,并且在机器上安装Word2002,否则导出将失败

转载于:https://www.cnblogs.com/zjx-sir/archive/2008/07/09/1238960.html

你可能感兴趣的文章
Maven 常用配置
查看>>
Objects源码解析
查看>>
video
查看>>
栈的c语言顺序实现(动态申请空间)
查看>>
【转】 Pro Android学习笔记(六七):HTTP服务(1):HTTP GET
查看>>
获取子iframe框架的元素
查看>>
WordCount bug修复录
查看>>
承载进程 (vshost.exe)
查看>>
[转]WPF MVVM 实战
查看>>
[转载] Python 标准库 urllib2 的使用细节
查看>>
Silverlight使用DataGrid的模板列(DataGridTemplateColumn)实现类似TreeListView控件的效果
查看>>
Java学习——Applet写字符串(调字体)
查看>>
react路由
查看>>
nyoj 220——推桌子——————【贪心】
查看>>
java 静态方法分析
查看>>
codevs——4189 字典&&HihoCoder #1014 : Trie树
查看>>
洛谷——P1602 Sramoc问题
查看>>
【MySQL笔记】字符串、时间日期转换
查看>>
jQuery实战之仿淘宝商城左侧导航效果
查看>>
AC日记——「SCOI2016」幸运数字 LiBreOJ 2013
查看>>