波斯马BOSSMA Information Technology

使用myxls导出真正的Excel文件

发布时间:2010年1月31日 / 分类:ASP.NET / 14,972 次浏览 / 评论

使用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第二行”);

本博客所有文章如无特别注明均为原创。
复制或转载请以超链接形式注明转自波斯马,原文地址《使用myxls导出真正的Excel文件

关键字:

建议订阅本站,及时阅读最新文章!
【上一篇】 【下一篇】

发表评论