ASP.NET(C#)将数据导出到Word或Excel
1) ASP.NET(C#)将数据导出到Word或Excel
命名空间:
using System.IO;
using System.Text;
将DataGrid的数据导出到Excel
string excelname="excel文件名";
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.AppendHeader("Content-disposition", "attachment;filename=" + excelname + ".xls");
dr1.Page.EnableViewState = false;
StringWriter sw = new StringWriter();
HtmlTextWriter tw = new HtmlTextWriter(sw);
dr1.RenderControl(tw);
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
将DataGrid的数据导出到Word
string excelname="word文件名";
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
HttpContext.Current.Response.ContentType = "application/ms-winword";
HttpContext.Current.Response.AppendHeader("Content-disposition", "attachment;filename=" + excelname + ".doc");
dr1.Page.EnableViewState = false;
StringWriter sw = new StringWriter();
HtmlTextWriter tw = new HtmlTextWriter(sw);
dr1.RenderControl(tw);
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
ASP.NET 2.0,C#----利用GridView控件导出其他文件(导出Excel,导出Word文件)
// 注意,在Visual Studio2005平台下,如果使用GridView导出文件,
//就必须重载VerifyRenderingInServerForm方法
public override void VerifyRenderingInServerForm(Control control)
{
}
/// <summary>
/// 导出到文件的方法,
/// </summary>
/// <param name="Model">Model=1:导出为Execl,Model=2:导出为Word </param>
private void toFiles(int Model)
{
string strFileName = DateTime.Now.ToString("yyyyMMdd-hhmmss");
System.Web.HttpContext HC = System.Web.HttpContext.Current;
HC.Response.Clear();
HC.Response.Buffer = true;
HC.Response.ContentEncoding = System.Text.Encoding.UTF8;//设置输出流为简体中文
if (Model == 1)
{
//---导出为Excel文件
HC.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8) + ".xls");
HC.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
}
else
{
//--- 导出为Word文件
HC.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8) + ".doc");
HC.Response.ContentType = "application/ms-word";//设置输出文件类型为Word文件。
}
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
this.GridView1.RenderControl(htw);
HC.Response.Write(sw.ToString());
HC.Response.End();
}
//-导出为Excel文件
protected void ToExecl_Click(object sender, EventArgs e)
{
toFiles(1);
}
//-导出为Word文件
protected void Button1_Click(object sender, EventArgs e)
{
toFiles(2);
}
(2)
private byte [] CreateDOC ( Dictionary<string, string> dicHeader, Dictionary<int, Dictionary<string, string>> dicProductCollection ) {
........
........
........
string fileName = path;
if ( File.Exists ( fileName ) ) { //图片存在就加进去
object linkToFile = false;
object saveWithDocument = true;
b = wordApp.Selection.Find.Execute (
ref from, 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 );
wordApp.Selection.Text = "";
object anchor = wordDoc.Application.Selection.Range;
wordDoc.Application.ActiveDocument.InlineShapes.AddPicture ( fileName, ref linkToFile, ref saveWithDocument, ref anchor ); // 添加图片
float [] arrFix = GetFixImage ( fileName, 180 );
if ( arrFix [0] != 0 && arrFix [1] != 0 ) { //设置宽度
wordDoc.Application.ActiveDocument.InlineShapes [pictureCount].Width = arrFix [0];//图片宽度
wordDoc.Application.ActiveDocument.InlineShapes [pictureCount].Height = arrFix [1];//图片高度
}
pictureCount++;
} else {
to = "";
b = wordApp.Selection.Find.Execute (
ref from, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref to,
ref replaceOne, ref Nothing, ref Nothing, ref Nothing, ref Nothing ); //替换掉#image
}
}
}
}
#endregion
string folder = ConfigurationManager.AppSettings ["Folder"];
string outPath = Server.MapPath ( folder );
object name = DateTime.Now.Year.ToString () + "_" + DateTime.Now.Month.ToString () + "_" + DateTime.Now.Day.ToString () +
"_" + DateTime.Now.Hour.ToString () + "_" + DateTime.Now.Minute.ToString () + "_" + DateTime.Now.Second.ToString () + ".doc";
object filename = outPath + "\\" + name;
if ( !Directory.Exists ( outPath ) ) {
Directory.CreateDirectory ( outPath );
}
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 );
GC.Collect ();
byte [] bytes = File.ReadAllBytes ( filename.ToString () );
return bytes;
} catch ( SystemException ee ) {
log.Error ( "OfferSheet Word Error:" + ee.ToString () );
GC.Collect ();
return null;
}
}
private float [] GetFixImage ( string fileName ,float fixNumber) {
float [] arrFix = new float [2];
System.Drawing.Image imageCheck = System.Drawing.Image.FromFile ( fileName );
if ( Convert.ToInt32 ( imageCheck.Width ) > 400 || Convert.ToInt32 ( imageCheck.Height ) > 400 ) {
if ( imageCheck.Width > imageCheck.Height ) { // 宽》高
arrFix [0] = fixNumber;
arrFix [1] = fixNumber * Convert.ToInt32 ( imageCheck.Height ) / Convert.ToInt32 ( imageCheck.Width );
} else {
arrFix [0] = fixNumber * Convert.ToInt32 ( imageCheck.Width ) / Convert.ToInt32 ( imageCheck.Height );
arrFix [1] = fixNumber;
}
} else {
arrFix [0] = 0;
arrFix [1] = 0;
}
return arrFix;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗