李晓亮的博客

导航

【转】使用myxls导出真正的Excel文件

转自:http://blog.bossma.cn/dotnet/myxls-export-excel/

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

posted on 2011-06-13 01:17  LeeXiaoLiang  阅读(343)  评论(0)    收藏  举报