如何把Web页面上的某一个DataGrid对应的DataSet中的数据导出到一个Word文件中?
哪位做过指点指点,20分奉上。
哪位做过指点指点,20分奉上。
解决方案 »
- eWebEditor如果修改(提高)缩略图的质量?
- gridview的问题???????
- 急!编写下载代码的一些问题
- jatoolsPrinter打印问题
- asp.net 2.0 中怎么加 global.asax 文件
- 有关DropDownList控件问题求教
- 新人刚学asp,按教程做显示FROM 子句语法错误,求指导
- 如何实现在进入一个页面之前,给用户有点提示(如显示 页面正在加载中...请稍等)
- 又是一个数据更新问题!谁会?
- 菜问题,在其他机子好的,在我的计算机上出现““/”应用程序中的服务器错误。”
- 如何将计算结果四舍五入的保留两位小数?急!
- 如何将DataTable中的数据绑定到Table中
word的对象模型你可以早word2003上查到!
vb.net的例子
private static void WriteDataGridToWord(string fileName, string tTitle, DataGrid dg, string[] invisibleColumns)
{
if (invisibleColumns != null)
{
//隐藏 invisibleColumns[] 这些列
for (int i = 0; i < invisibleColumns.Length; i++)
{
dg.Columns[Convert.ToInt32(invisibleColumns[i])].Visible = false;
}
} //要显示的列数
int dgColVisibleCount = 0;
for (int i = dg.Columns.Count - 1; i >= 0 ; i--)
{
if (dg.Columns[i].Visible == true)
{
dgColVisibleCount++;
}
} Word.Application wordApp = new Word.ApplicationClass();
Word.Document myDoc;
Word.Table wordTable; object missing = System.Reflection.Missing.Value; int rowIndex, colIndex;
rowIndex = 1;
colIndex = 0; myDoc = wordApp.Documents.Add(ref missing, ref missing,ref missing, ref missing);
myDoc.Activate(); //标题
wordApp.Selection.TypeText(tTitle);
wordApp.Selection.TypeParagraph(); //记录数
wordApp.Selection.TypeText("记录总数:" + dg.Items.Count);
wordApp.Selection.TypeParagraph(); //数据表格
Object start = wordApp.Selection.Start;
Object end = wordApp.Selection.End; wordTable = myDoc.Tables.Add(myDoc.Range(ref start, ref end),
dg.Items.Count + 1,
dgColVisibleCount,
ref missing,
ref missing); //列标题
foreach (DataGridColumn col in dg.Columns)
{
if (col.Visible == true)
{
colIndex++;
wordTable.Cell(1, colIndex).Range.InsertAfter(col.HeaderText);
}
}
foreach (DataGridItem item in dg.Items)
{
rowIndex++;
colIndex = 0; for (int j = 0; j < dg.Columns.Count; j++)
{
if (dg.Columns[j].Visible == true)
{
colIndex++;
wordTable.Cell(rowIndex, colIndex).Range.InsertAfter(item.Cells[j].Text);
}
}
} wordTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
wordTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
wordApp.Visible = true; object wordFileName = fileName; myDoc.SaveAs(ref wordFileName, 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); //关闭myWordDoc文档对象
myDoc.Close(ref missing, ref missing, ref missing);
//关闭wordApp组件对象
wordApp.Application.Quit(ref missing, ref missing, ref missing); if (invisibleColumns != null)
{
//恢复 invisibleColumns[] 这些列的显示
for (int i = 0; i < invisibleColumns.Length; i++)
{
dg.Columns[Convert.ToInt32(invisibleColumns[i])].Visible = true;
}
}
}
{
if (invisibleColumns != null)
{
//隐藏 invisibleColumns[] 这些列
for (int i = 0; i < invisibleColumns.Length; i++)
{
dg.Columns[Convert.ToInt32(invisibleColumns[i])].Visible = false;
}
} HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
page.Response.ContentType = "application/vnd.ms-word" ;//指定生成文件的类型
HttpContext.Current.Response.Charset = "";
if (encoding != null)
HttpContext.Current.Response.ContentEncoding = encoding;
else
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default; dg.Page.EnableViewState = false; System.IO.StringWriter tw=new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw=new System.Web.UI.HtmlTextWriter(tw); //标题
title += "<p></p><p></p>"; //总记录数requestEncoding
string totalCount = "<p>总记录数:" + dg.Items.Count + "</p>";// + HttpContext.Current.Response.ContentEncoding.EncodingName dg.RenderControl(hw);
HttpContext.Current.Response.Write(title + totalCount + tw.ToString());//写入word文件
HttpContext.Current.Response.End(); if (invisibleColumns != null)
{
//恢复 invisibleColumns[] 这些列
for (int i = 0; i < invisibleColumns.Length; i++)
{
dg.Columns[Convert.ToInt32(invisibleColumns[i])].Visible = true;
}
} }