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,否则导出将失败
从sql里面取出datatable你会的吧?
DataTable dataTable = iParameter.GetDataTable("***");
//然后创建Word文档
Word.Application WordApp = new Word.ApplicationClass();
Word.Document WordDoc = WordApp.Documents.Add( ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//创建表格
Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 12 , 3 , ref Nothing, ref Nothing);
//填充数据
foreach(DataRow row in dataTable.Rows)
{
for(i=1;i<...;...)
{
for(j=1;j<...;...)//不具体写了,大概就是利用横纵坐标或者数组列名for循环在表格中定位
{
newTable.Cell( i, j ).Range.Text = row["列名"].ToString();//列名如果多个,可以用一个数组代替其中一个for循环,总之就是横纵坐标或者列名循环定位,这块自己琢磨怎么循环,想不出来再问我,我再帮你仔细想想
}
}
}
你把ACCESS输出到网页没有问题吧?
那就在文件头加上:
Response.Buffer = True
response.ContentType ="Application/msword"
这样就生成WORD了。