首页 新闻 会员 周边 捐助

asp.net 导出到excle里面

0
[已解决问题] 解决于 2012-04-24 13:54

最近做程序,需要做一个导出2003excle的功能,

做了个效果,感觉不太好,希望有过这方面经验的朋友,提供一些源码,

是源码,不要给我链接。

备注:使用vs2005 sql2000/2005,我到最后是保存到DataSet里面了。

最近问问题都很少有人来解决,如果有人解决了,再给分,谢谢!

dinoy的主页 dinoy | 初学一级 | 园豆:160
提问于:2011-07-27 15:50
< >
分享
最佳答案
0

http://blog.csdn.net/jilm168/article/details/1869118 这里有,方法很多,供参考

奖励园豆:5
麦田里的守望者 | 菜鸟二级 |园豆:428 | 2011-07-27 17:11
其他回答(1)
0

用了好多种方案,最后发现NPOI是最好使的,下面这段代码是从我的项目中扣下来了,给你提供个思路

HSSFWorkbook workbook = new HSSFWorkbook();
Sheet sheet
= workbook.CreateSheet(string.Format("{0}.{1}.{2} - {3}.{4}.{5}", startDate.Year, startDate.Month, startDate.Day, endDate.Year, endDate.Month, endDate.Day));
var style1
= workbook.CreateCellStyle();
style1.BorderBottom
= style1.BorderLeft = style1.BorderRight = style1.BorderTop = CellBorderType.THIN;
style1.BottomBorderColor
= style1.RightBorderColor = style1.LeftBorderColor = style1.TopBorderColor = HSSFColor.BLACK.index;
var style2
= workbook.CreateCellStyle();
style2.BorderBottom
= style2.BorderLeft = style2.BorderRight = style2.BorderTop = CellBorderType.THIN;
style2.FillForegroundColor
= NPOI.HSSF.Util.HSSFColor.GREY_25_PERCENT.index;
style2.FillPattern
= FillPatternType.SOLID_FOREGROUND;
for (int i = 0; i <= reportData.Count; i++)
{
var row
= sheet.CreateRow(i);
for (int j = 0; j < 29; j++)
{
Cell cell
= row.CreateCell(j);

cell.CellStyle
= style1;
switch (j)
{
case 0:
{
cell.SetCellValue(reportData[i
- 1].DeptName);
break;
}
case 1:
{
cell.CellStyle
= style2;
cell.SetCellValue(reportData[i
- 1].DeptEmpNumber);
break;
}
case 2:
{
cell.SetCellValue(reportData[i
- 1].IDLN);
break;
}
}

}
}

if (context == null)
throw new ApplicationException("Current System.Web.HttpContext not found - Send failed.");

if (!context.Response.Buffer)
{
context.Response.Buffer
= true;
context.Response.Clear();
}
MemoryStream ms
= new MemoryStream();
workbook.Write(ms);
context.Response.ContentType
= "application/vnd.ms-excel";
context.Response.AddHeader(
"Content-Disposition", string.Format("{0};filename={1}_{2}_{3}.xls", "attachment", "MonthlyReport", startDate.ToShortDateString(), endDate.ToShortDateString()));
context.Response.Flush();
context.Response.BinaryWrite(ms.ToArray());

workbook
= null;
ms.Close();
ms.Dispose();

think8848 | 园豆:374 (菜鸟二级) | 2011-07-28 10:44
看了你的代码,你是用vs2008开发的吧,2005里面var好像使用不了。不过还是谢谢你,我再仔细看看,能不能给我一些帮助。
支持(0) 反对(0) dinoy | 园豆:160 (初学一级) | 2011-07-29 16:42
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册