1.先创建一个DataTable,作为数据来源,也可以另将其它的数据源。 Private Function CreaTable() As DataTable Dim dt As New DataTable() dt.Columns.Add("列1", GetType(String)) dt.Columns.Add("列2", GetType(Integer)) dt.Columns.Add("列3", GetType(String)) dt.Columns.Add("列4", GetType(String)) Dim row, row1 As DataRow row = dt.NewRow() row!列1 = "行1" row!列2 = 1 row!列3 = "d" row!列4 = "a" dt.Rows.Add(row) row1 = dt.NewRow() row1!列1 = "行2" row1!列2 = 12 row1!列3 = "b" row1!列4 = "c" dt.Rows.Add(row1) Return dt End Function可以把datagrid作为数据源2.将表中的内容导出到WORD Dim wordApp As New Word.Application() Dim myDoc As Word.Document Dim oTable As Word.Table Dim rowIndex, colIndex As Integer rowIndex = 1 colIndex = 0 wordApp.Documents.Add() myDoc = wordApp.ActiveDocument Dim Table As New DataTable() Table = CreaTable() oTable = myDoc.Tables.Add(Range:=myDoc.Range(Start:=0, End:=0), NumRows:=Table.Rows.Count + 1, NumColumns:=Table.Columns.Count) '将所得到的表的列名,赋值给单元格 Dim Col As DataColumn Dim Row As DataRow For Each Col In Table.Columns colIndex = colIndex + 1 oTable.Cell(1, colIndex).Range.InsertAfter(Col.ColumnName) Next '得到的表所有行,赋值给单元格 For Each Row In Table.Rows rowIndex = rowIndex + 1 colIndex = 0 For Each Col In Table.Columns colIndex = colIndex + 1 oTable.Cell(rowIndex, colIndex).Range.InsertAfter(Row(Col.ColumnName)) Next Next oTable.Borders.InsideLineStyle = 1 oTable.Borders.OutsideLineStyle = 1 wordApp.Visible = True
/// <summary>
/// 数据集生成Word文件
/// </summary>
private void ToWord()
{
object Missing = System.Reflection.Missing.Value;
int NumRows, NumColumns, rowIndex, colIndex;
object FileName = "c:\\liu.doc"; //保存word文件的路径
// Office.CommandBar MyBar; //工具栏
Word.ApplicationClass wordApp = new Word.ApplicationClass();
Word.Document myDoc;
Word.Table oTable;
DataTable Table = new DataTable(); rowIndex = 1;
colIndex = 0;
wordApp.Documents.Add(ref Missing,ref Missing,ref Missing, ref Missing);
myDoc = wordApp.ActiveDocument;
Table = MyBind();
oTable = myDoc.Tables.Add(myDoc.Range(ref Missing, ref Missing),NumRows=Table.Rows.Count + 1, NumColumns=Table.Columns.Count,ref Missing,ref Missing); //将所得到的表的列名,赋值给单元格
foreach(DataColumn Col in Table.Columns)
{
colIndex = colIndex + 1;
oTable.Cell(1, colIndex).Range.InsertAfter(Col.ColumnName);
} //得到的表所有行,赋值给单元格
foreach(DataRow Row in Table.Rows)
{
rowIndex = rowIndex + 1;
colIndex = 0;
foreach(DataColumn Col in Table.Columns)
{
colIndex = colIndex + 1;
oTable.Cell(rowIndex, colIndex).Range.InsertAfter(Row[Col.ColumnName].ToString());
}
} oTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleDashDot;
oTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleDot;
wordApp.Visible = false; //true:word文件显示 false:word文件不显示
myDoc.SaveAs(ref FileName,ref Missing,ref Missing,ref Missing,ref Missing,ref Missing,ref Missing,ref Missing,ref Missing,ref Missing,ref Missing);
}
#endregion
#region DataGridToWord
/// <summary>
/// DataGridToWord
/// </summary>
/// <param name="ctl">DataGrid控件</param>
private void DataGridToWord(System.Web.UI.Control ctl)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset ="";
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=liu0910.doc");
HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.GetEncoding("GB2312");
//更改ContentType的值为ms-word即可实现导出到Word
HttpContext.Current.Response.ContentType ="application/ms-word";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
ctl.Page.EnableViewState =false;
System.IO.StringWriter tw = new System.IO.StringWriter() ;
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
#endregion