封装类 导出gridview为word
来源:互联网 发布:电视软件应用市场 编辑:程序博客网 时间:2024/05/29 21:34
public static void Export_Word(Page page, GridView gv, string Title, int ColumnCount)
{
int RowCount = gv.Rows.Count + 2;
object missing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Application myword_app = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document mydoc = new Microsoft.Office.Interop.Word.Document();
myword_app.Documents.Add(ref missing, ref missing, ref missing, ref missing);
mydoc = myword_app.ActiveDocument;
Microsoft.Office.Interop.Word.Table table;
table = mydoc.Tables.Add(myword_app.Selection.Range, RowCount, ColumnCount, ref missing, ref missing);
#region 标题
table.Cell(1, 1).Range.Text = Title;
table.Cell(1, 1).Row.Height = 30;
table.Cell(1, 1).Range.Font.Size = 16;
table.Cell(1, 1).Range.Font.Name = "华文中宋";
//Microsoft.Office.Interop.Word.Range MergerRange = new Microsoft.Office.Interop.Word.Range(table.Cell[1, 1],table(1,ColumnCount));
if (table.Rows[1].Cells.Count > 1)
//table.Rows[1].Range.Move((Object)table.Cell(1, 2), (Object)(ColumnCount - 1));
table.Rows[1].Cells.Merge();
#endregion
#region 表列名
for (int i = 1; i <= ColumnCount; i++)
{
table.Cell(2, i).Range.Text = gv.HeaderRow.Cells[i - 1].Text;
}
#endregion
#region 填充表内容
for (int i = 0; i < gv.Rows.Count; i++)
for (int j = 0; j < ColumnCount; j++)
{
try
{
table.Cell((i + 3), (j + 1)).Range.Text = ((TextBox)(gv.Rows[i].Cells[j].Controls[1])).Text;
}
catch
{
table.Cell((i + 3), (j + 1)).Range.Text = gv.Rows[i].Cells[j].Text;
}
}
#endregion
myword_app.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
myword_app.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
#region 保存
string path = page.Server.MapPath(".");
string filename = Title + ".doc";
object filepath = path + "//" + filename;
mydoc.SaveAs(ref filepath, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
mydoc.Close(ref missing, ref missing, ref missing);
myword_app.Quit(ref missing, ref missing, ref missing);
#endregion
#region 下载
FileInfo fileInfo = new FileInfo((string)filepath);
HttpResponse Response = page.Response;
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, Encoding.UTF8).ToString());
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = "application/octet-stream";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.WriteFile(fileInfo.FullName);
Response.Flush();
System.IO.FileInfo file = new System.IO.FileInfo((string)filepath);
if (file.Exists)
{
file.Delete();
}
Response.End();
#endregion
}
- 封装类 导出gridview为word
- 封装类 导出gridview为excel
- 把GridView的列表数据导出为Word表格文件
- GridView导出Excel,Word
- gridview 导出到word
- GridView导出到Word
- GridView导出到Word
- GridView导出Excel与Word
- Gridview导出为Excel
- GridView 导出为Excel
- html导出为word
- 扩展GridView控件(8) - 导出数据源的数据为Excel、Word或Text
- 扩展GridView控件(8) - 导出数据源的数据为Excel、Word或Text
- 将 GridView 导出 为 excel
- GridView导出为Excel文档
- 将gridview导出为Excel
- GridView数据导出到Word/Excel中
- GridView导出到Excel或Word文件
- [推荐] 为啥大家对外包那么排斥呢?
- pidgin 聊天软件
- 深入Android
- 星光9200开发板的BSP移植到Vx6.7
- 同余幂的求解算法(原)
- 封装类 导出gridview为word
- Net4.0---Framwork新增特性
- netscreen防火墙端口映射(vip)
- WPF(四)举例讲解WPF控件模版
- 再说接入点
- Changing Status pane text and icon
- Linux 线程同步---条件变量
- Linux 编程经典书籍推荐
- 顯式調用DLL