从数据库中读出的数据怎样能转换成word,求助!!!
解决方案 »
- DataTable移除Row 在线等 很急!!!
- C# ListBox,ComboBox,DataGridView应用VScrollBar疑惑。
- ViewState保存数据怎么用?请大家帮我一下[寻求sp1234热心帮助]
- c#中我生成项目时报错???
- 如何在VS2005中动态生成一个统计结果表?并能保存为EXCL文件?打印?
- 请教,怎么在winform中将web中的cookie提交上去
- 我用winApi读配置文件(.ini文件),怎么读不出内容?
- 关于无法取到DATALIST中编辑模板中的控件的问题
- 有没有办法截获WebBrowser控件向服务器发送的请求以及接收到的响应信息
- 用c#开发基于数据库的软件方便吗?
- windows程序,这样从绑定dgv里面删除当前行可以吗?
- 遇到多点委托参数不同的问题?
/// <summary>
/// dataset数据导入Word
/// </summary>
/// <param name="ds"></param>
/// <param name="FileName"></param>
/// <param name="Titlename"></param>
public void DataSetToWord(DataSet ds, string FileName, string Titlename)
{
string ExportFileName = null;
if (FileName == null || FileName == "")
{
ExportFileName = "DFSOFT";
}
else
{
ExportFileName = FileName;
}
if (Titlename == "" || Titlename == null)
{
Titlename = "添加标题处";
} System.Data.DataTable dt = ds.Tables[0];
DataRow[] myRow = dt.Select();
int i = 0;
int cl = dt.Columns.Count;
//Web页面定义
HttpResponse resp;
resp=HttpContext.Current.Response;
resp.Clear();
resp.ContentEncoding=System.Text.Encoding.GetEncoding("utf-8");
resp.AppendHeader("Content-disposition","attachment;filename="+ExportFileName+".doc");
resp.ContentType="application/vnd.ms-word";
string BeginTab="<table border='0' cellpadding='0' cellspacing='0'style='border-right:#000000 0.1pt solid;border-top:#000000 0.1pt solid;'>";
string EndTab="</table>";
string FileIO=null;
string MainIO=null;
string TitleTab = "<tr><td style='font-size:30px;'align='center'><b>" + Titlename + "</b></td></tr><tr><td align='right' style='font-size:15px;'>" + DateTime.Now.Year.ToString() + "年" + DateTime.Now.Month.ToString() + "月" + DateTime.Now.Day.ToString() + "日 </td></tr>";
string BeginTr="<tr>";
string EndTr="</tr>";
for(i=0;i<cl;i++)
{
FileIO+="<td style='border-left:#000000 0.1pt solid; border-bottom:#000000 1.0pt solid; font-size:15px;' align='center'><b>"+dt.Columns[i].Caption.ToString()+"</b></td>"; }
FileIO=BeginTr.ToString()+FileIO.ToString()+EndTr.ToString(); //逐行处理数据
foreach(DataRow row in myRow)
{
string OutIO=null;
//当前数据写入
for(i=0;i<cl;i++)
{
OutIO+="<td style='border-left:#000000 0.1pt solid;border-bottom:#000000 1.0pt solid; font-size:15px;' align='center'>"+row[i].ToString()+"</td>"; }
MainIO+=BeginTr.ToString()+OutIO.ToString()+EndTr.ToString();
} FileIO="<center><table>"+TitleTab.ToString()+"<tr>"+BeginTab.ToString()+FileIO.ToString()+MainIO.ToString()+EndTab.ToString()+"</tr></table></center>";
resp.Write(FileIO.ToString());
resp.End();
//Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();
//Microsoft.Office.Interop.Word.Document doc;
//object nothing = System.Reflection.Missing.Value;
//doc = word.Documents.Add(ref nothing, ref nothing, ref nothing, ref nothing);
//doc.Paragraphs.Last.Range.Copy = FileIO;
//object myfilename = FileName;
//doc.Save();
////doc.SaveAs(ref myfilename, 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);
//doc.Close(ref nothing, ref nothing, ref nothing);
//word.Quit(ref nothing, ref nothing, ref nothing);
你试试这个!