Asp.Net导出Excel表格
public void ExportExcel(string sql)
{
using (SqlDataReader reader = DTcms.DBUtility.DbHelperSQL.ExecuteReader(sql))
{
if (reader.HasRows)
{
IWorkbook wk = new HSSFWorkbook();
ISheet sheet = wk.CreateSheet("Sheet2");
int rowIndex = 0;
NPOI.SS.UserModel.IRow row1 = sheet.CreateRow(rowIndex);
row1.HeightInPoints = 25;
sheet.SetColumnWidth(0, 35 * 256);
row1.CreateCell(0).SetCellValue("网址");
rowIndex++;
string str = "http://video.kaola100.com/yxsp/yjjz/";
while (reader.Read())
{
IRow row = sheet.CreateRow(rowIndex);
row.HeightInPoints = 20;
ICell cell = row.CreateCell(0);
cell.SetCellValue(str + reader.GetInt32(0).ToString());
rowIndex++;
}
MemoryStream ms = new MemoryStream();
wk.Write(ms);
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.ContentType = "application/x-xls";
Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}.xls", DateTime.Now.ToString("yyyyMMddHHmmssfff")));
// Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xls", HttpUtility.UrlEncode(DateTime.Now.ToString("yyyy-MM-dd"), System.Text.Encoding.UTF8)));
Response.BinaryWrite(ms.ToArray());
wk = null;
ms.Close();
ms.Dispose();
}
}
}