如何实现C#与WORD的导入导出?

我使用的是VS2008,SQLServer2005我想用编写个C/程序,可以实现程序与WORD按照拍版导入导出,请问可以实现吗?请指教谢谢!... 我使用的是VS2008,SQL Server2005 我想用编写个C/程序,可以实现程序与WORD按照拍版导入导出,请问可以实现吗?请指教 谢谢! 展开
 我来答
feisany
2008-10-24 · 超过36用户采纳过TA的回答
知道答主
回答量:141
采纳率:0%
帮助的人:0
展开全部
我这有对EXCEL的操作希望对你有帮助
#region -------------------------------------文件操作函数---------------------------------------
/// <summary>
/// 按照SQL的结果集生成Excel文件
/// </summary>
/// <param name="str_SQL">Sql语句</param>
/// <param name="FileName">Excel文件名</param>
public void WriteToExcel(string str_SQL,string FileName)
{
string strCols = "";
string strLine = "";

myAdapter = new SqlDataAdapter(str_SQL,myConnection);
ds = new DataSet();
myAdapter.Fill(ds);
DataTable dt = ds.Tables[0];

sw=new StringWriter();
//写入字段名
foreach(DataColumn myCol in dt.Columns)
{
strCols = strCols + myCol.ColumnName +"\t";
}
sw.WriteLine(strCols);

//写入数据
foreach(DataRow dr in dt.Rows)
{
foreach(DataColumn myCol in dt.Columns)
{
if( myCol.ColumnName == "身份证号")
{
strLine = strLine + "=RIGHT(\"'" + dr[myCol] +"\",LEN(\"" +dr[myCol] + "\"))" +"\t";
continue;
}
if (myCol.ColumnName == "纳税人识别号")
{
strLine = strLine + "=RIGHT(\"'" + dr[myCol] + "\",LEN(\"" + dr[myCol] + "\"))" + "\t";
continue;
}

strLine = strLine + dr[myCol] +"\t";
}
strLine = strLine + "\r\n";
}
sw.WriteLine(strLine);

dt.Dispose();
ds.Dispose();

//生成Excel文件
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename="+ HttpUtility.UrlEncode(FileName,System.Text.Encoding.UTF8 ));
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.Write(sw);
HttpContext.Current.Response.End();

sw.Close();
}
public void WriteToExcel(string str_SQL,string FileName,string Title)
{
string strCols = "";
string strLine = "";
string strTemp ="\t\t";

myAdapter = new SqlDataAdapter(str_SQL,myConnection);
ds = new DataSet();
myAdapter.Fill(ds);
DataTable dt = ds.Tables[0];

sw=new StringWriter();
//写入文件头
strTemp += Title;
sw.WriteLine(strTemp);
//写入字段名
foreach(DataColumn myCol in dt.Columns)
{
strCols = strCols + myCol.ColumnName +"\t";
}
sw.WriteLine(strCols);

//写入数据
foreach(DataRow dr in dt.Rows)
{
foreach(DataColumn myCol in dt.Columns)
{
if (myCol.ColumnName == "身份证号")
{
strLine = strLine + "=RIGHT(\"'" + dr[myCol] + "\",LEN(\"" + dr[myCol] + "\"))" + "\t";
continue;
}
if (myCol.ColumnName == "纳税人识别号")
{
strLine = strLine + "=RIGHT(\"'" + dr[myCol] + "\",LEN(\"" + dr[myCol] + "\"))" + "\t";
continue;
}
strLine = strLine + dr[myCol] +"\t";
}
strLine = strLine + "\r\n";
}
sw.WriteLine(strLine);

dt.Dispose();
ds.Dispose();

//生成Excel文件
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename="+ HttpUtility.UrlEncode(FileName,System.Text.Encoding.UTF8 ));
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.Write(sw);
HttpContext.Current.Response.End();

sw.Close();
}
#endregion
原听然015
2008-10-24 · TA获得超过2415个赞
知道小有建树答主
回答量:1390
采纳率:100%
帮助的人:1139万
展开全部
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8));
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentType = "application/word";//设置输出文件类型为word文件。
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
xxxx.RenderControl(oHtmlTextWriter);//你要导出的数据

HttpContext.Current.Response.Write(oStringWriter.ToString());

HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();
HttpContext.Current.Response.End();
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
SantiCazorla
2008-10-24 · 超过21用户采纳过TA的回答
知道答主
回答量:175
采纳率:0%
帮助的人:0
展开全部
关注
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式