把DataTable导出到EXCEL

    /// <summary>
    /// 把DataTable导出到EXCEL
    /// </summary>
    /// <param name="reportName">报表名称</param>
    /// <param name="dataTable">数据源表</param>
    /// <param name="saveFileName">Excel全路径文件名</param>
    /// <returns>导出是否成功</returns>

    public bool ExportExcel(string reportName, System.Data.DataTable dataTable, string saveFileName)
    {
        if (dataTable == null)
        {
            returnStatus = -1;
            returnMessage = "数据集为空!";
            return false;
        }

        bool fileSaved = false;
        Excel.Application xlApp = new Excel.Application();
        if (xlApp == null)
        {
            returnStatus = -1;
            returnMessage = "无法创建Excel对象,可能您的计算机未安装Excel!";
            return false;
        }

        Workbooks workbooks = xlApp.Workbooks;
        Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
        Worksheet worksheet = (Worksheet)workbook.Worksheets[1];//取得sheet1
        worksheet.Cells.Font.Size = 10;
        Range range;

        long totalCount = dataTable.Rows.Count;
        long rowRead = 0;
        float percent = 0;

        worksheet.Cells[1, 1] = reportName;
        ((Range)worksheet.Cells[1, 1]).Font.Size = 12;
        ((Range)worksheet.Cells[1, 1]).Font.Bold = true;

        for (int i = 0; i < dataTable.Columns.Count; i++)
        {
            worksheet.Cells[2, i + 1] = dataTable.Columns[i].ColumnName;
            range = (Range)worksheet.Cells[2, i + 1];
            range.Interior.ColorIndex = 15;
            range.Font.Bold = true;

        }

        for (int r = 0; r < dataTable.Rows.Count; r++)
        {
            for (int i = 0; i < dataTable.Columns.Count; i++)
            {
                worksheet.Cells[r + 3, i + 1] = dataTable.Rows[r][i].ToString();
            }
            rowRead++;
            percent = ((float)(100 * rowRead)) / totalCount;
        }

        range = worksheet.get_Range(worksheet.Cells[2, 1], worksheet.Cells[dataTable.Rows.Count + 2, dataTable.Columns.Count]);
        range.BorderAround(XlLineStyle.xlContinuous, XlBorderWeight.xlThin, XlColorIndex.xlColorIndexAutomatic, null);
        if (dataTable.Rows.Count > 0)
        {
            range.Borders[XlBordersIndex.xlInsideHorizontal].ColorIndex = XlColorIndex.xlColorIndexAutomatic;
            range.Borders[XlBordersIndex.xlInsideHorizontal].LineStyle = XlLineStyle.xlContinuous;
            range.Borders[XlBordersIndex.xlInsideHorizontal].Weight = XlBorderWeight.xlThin;
        }
        if (dataTable.Columns.Count > 1)
        {
            range.Borders[XlBordersIndex.xlInsideVertical].ColorIndex = XlColorIndex.xlColorIndexAutomatic;
            range.Borders[XlBordersIndex.xlInsideVertical].LineStyle = XlLineStyle.xlContinuous;
            range.Borders[XlBordersIndex.xlInsideVertical].Weight = XlBorderWeight.xlThin;
        }

        if (string.IsNullOrEmpty(saveFileName))
        {
            try
            {
                workbook.Saved = true;
                workbook.SaveCopyAs(saveFileName);
                fileSaved = true;
            }
            catch (Exception ex)
            {
                fileSaved = false;
                returnStatus = -1;
                returnMessage = "导出文件时出错,文件可能正被打开!\n" + ex.Message;
            }
        }
        else
        {
            fileSaved = false;
        }

        if (range != null)
        {
            Marshal.ReleaseComObject(range);
            range = null;
        }
        if (worksheet != null)
        {
            Marshal.ReleaseComObject(worksheet);
            worksheet = null;
        }
        if (workbook != null)
        {
            Marshal.ReleaseComObject(workbook);
            workbook = null;
        }
        if (workbooks != null)
        {
            Marshal.ReleaseComObject(workbooks);
            workbooks = null;
        }
        xlApp.Application.Workbooks.Close();
        xlApp.Quit();
        if (xlApp != null)
        {
            Marshal.ReleaseComObject(xlApp);
            xlApp = null;
        }
        GC.Collect();
        return fileSaved;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值