第一种:
private void ExportResult()
{
try
{
string filename = "text.xls";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.AppendHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";//"application/xhtml+xml";
StringWriter stringWrite = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWrite);
HttpContext.Current.Response.Write(stringWrite.ToString());
HttpContext.Current.Response.End();
}
catch (Exception ex)
{
setMsg(1, "EXCELファイルダウンロード処理に異常が発生しました。(" + ex.Message.ToString() + ")");
}
}
加以下代码则不会报以上错误:(取消对GridView1控件验证的方法)
public override void VerifyRenderingInServerForm(Control control)
{
if (!control.GetType().Equals(GridView1.GetType()))
{
base.VerifyRenderingInServerForm(control);
}
}
第二种:生成的Excel中插入图片
using NPOI.HPSF;
using NPOI.HSSF.UserModel;
using NPOI.HSSF.Util;
using NPOI.SS.UserModel;
using System.Data;
using System.Net;
using System.IO;
private void ExportResult(DataTable dt)
{
if (null != dt)
{
HSSFWorkbook hssfworkbook;
string filename = "aa.xls";
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", filename));
Response.Clear();
hssfworkbook = new HSSFWorkbook();
HSSFSheet excelSheet = (HSSFSheet)hssfworkbook.CreateSheet("StockReport_" + DateTime.Now.ToString("yyyyMMdd"));
HSSFPatriarch patriarch = (HSSFPatriarch)excelSheet.CreateDrawingPatriarch();//每个Excel只能创建一个画板
excelSheet.SetColumnWidth(0, 15 * 256);//设置列宽
Row rowHead = excelSheet.CreateRow(0);//创建第一行
//给第一行的列赋值
rowHead.CreateCell(0).SetCellValue("画像");
for (int i = 1; i < dt.Columns.Count + 1; i++)
{
rowHead.CreateCell(i).SetCellValue(dt.Columns[i - 1].ColumnName);
}
//循环数据列表写入到Excel
for (int i = 1; i < dt.Rows.Count + 1; i++)
{
Row row = excelSheet.CreateRow(i);
excelSheet.GetRow(i).Height = 78 * 20;
for (int j = 1; j < dt.Columns.Count + 1; j++)
{
row.CreateCell(j).SetCellValue(dt.Rows[i - 1][j - 1].ToString());
}
//第一个单元格添加图片
string ImgUrl = "http://test/images/" + dt.Rows[i - 1]["picSrc"];
//根据图片路径把图片保存到本地
WebClient wc = new WebClient();
string PICTURE_FILE_PATH = @"D:/testExcel/test.jpg";
wc.DownloadFile(ImgUrl, PICTURE_FILE_PATH);
wc.Dispose();
if (File.Exists(PICTURE_FILE_PATH))
{
//添加图片到指定单元格
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, 0, i, 0, i);//(0,0,0,0,起始列,起始行,结束列,结束行)图片的左上角-右下角
HSSFPicture picture;
int picMain = excelSheet.Workbook.AddPicture(File.ReadAllBytes(PICTURE_FILE_PATH), PictureType.JPEG);
picture = (HSSFPicture)patriarch.CreatePicture(anchor, picMain);
picture.LineStyle = HSSFPicture.LINESTYLE_NONE;
picture.Resize();//图片按原始大小显示
File.Delete(PICTURE_FILE_PATH);//删除本地生成的IMG
}
}
MemoryStream file = new MemoryStream();
hssfworkbook.Write(file);
Response.BinaryWrite(file.GetBuffer());
Response.End();
}
}
相关用例:http://www.cnblogs.com/tonyqus/archive/2009/04/12/1434209.html
Q网络硬盘:NPOI.dll