使用asp.net导出excel的方法有很多,我以前也介绍过一些。但是有些不能导出真正的excel文件,有些效率又差些。这次找到了一个更好的工具,可以导出真正的excel文件,效率也还不错,而且使用MIT协议可以在闭源的商业软件中免费使用,那就是:myxls。
1、首先下载:
下载地址:http://myxls.in2bits.org/Downloads.ashx
http://sourceforge.net/projects/myxls/files/
2、添加引用到你的网站或项目中:
3、一个导出excel的测试程序:
/// <summary>
/// 导出Excel
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ExportBtn_Click(object sender, EventArgs e)
{
XlsDocument xls = new XlsDocument();
xls.FileName = "TestList.xls";
int rowIndex = 1;
Worksheet sheet = xls.Workbook.Worksheets.Add("测试表");//Sheet名称
Cells cells = sheet.Cells;
Cell cell = cells.Add(1, 1, "编号");
cell.Font.Bold = true;
cell = cells.Add(1, 2, "名称");
cell.Font.Bold = true;
foreach (DataRow row in table.Rows)
{
cells.Add(rowIndex, 1, rowIndex);
cells.Add(rowIndex, 2, "名称"+rowIndex);
rowIndex++;
}
xls.Send();
}
看起来很简单,先试试看。还有很多的功能,自己慢慢发掘吧。
目前好像还不支持插入图片到单元格,CellTypes类型只有这几项:
public enum CellTypes
{
Error,
Null,
Integer,
Text,
Float,
Formula
}
如果需要在单元格中换行,可以使用:\r\n
cells.Add(rowIndex, 2, “第一行\r\n第二行”);
发表评论
相关文章
国内AI资源汇总,AI聊天、AI绘画、AI写作、AI视频、AI设计、AI编程、AI音乐等,国内顺畅访问,无需科学上网。
扫码或点击进入:萤火AI大全
文章分类
最新评论