如何实现C#与WORD的导入导出?
我使用的是VS2008,SQLServer2005我想用编写个C/程序,可以实现程序与WORD按照拍版导入导出,请问可以实现吗?请指教谢谢!...
我使用的是VS2008,SQL Server2005 我想用编写个C/程序,可以实现程序与WORD按照拍版导入导出,请问可以实现吗?请指教 谢谢!
展开
3个回答
展开全部
我这有对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
#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
展开全部
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();
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();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
关注
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |