如何使用适当的格式导出数据表并使用C#Microsoft.Office.Interop.Excel在Excel中写入文本?

时间:2012-10-03 13:02:08

标签: c# datatable formatting export-to-excel

我正在使用此功能将数据表导出到Excel中。

  protected void ExportExcel(System.Data.DataTable dt) 
    { 
         if (dt == null||dt.Rows.Count==0) return; 
         Microsoft.Office.Interop.Excel.Application xlApp = 
new Microsoft.Office.Interop.Excel.Application(); 

 if (xlApp == null) 
 { 
     return; 
 } 
 System.Globalization.CultureInfo CurrentCI = 
System.Threading.Thread.CurrentThread.CurrentCulture; 
 System.Threading.Thread.CurrentThread.CurrentCulture = 
    new System.Globalization.CultureInfo("en-US"); 
 Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks; 
 Microsoft.Office.Interop.Excel.Workbook workbook = 
 workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet); 
 Microsoft.Office.Interop.Excel.Worksheet worksheet = 
(Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1]; 
 Microsoft.Office.Interop.Excel.Range range; 
 long totalCount = dt.Rows.Count; 
 long rowRead = 0; 
 float percent = 0; 
 for (int i = 0; i < dt.Columns.Count; i++) 
 { 
     worksheet.Cells[1, i + 1] = dt.Columns[i].ColumnName; 
     range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, i + 1]; 
     range.Interior.ColorIndex = 15; 
     range.Font.Bold = true; 

 } 

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

 xlApp.Visible = true;
}

RESULT iMAGE

正如您在生成的Excel图像中看到的那样,列的格式不正确,即列大小不适应数据绑定项。如何根据数据表自动调整excel单元格?

我还想添加一些文本,也许是一个标题,比如说“System.DateTime.Now”或“数据表名称”。我该怎么做?

1 个答案:

答案 0 :(得分:6)

您应该可以像这样格式化列:

Microsoft.Office.Interop.Excel.Range columns = worksheet.UsedRange.Columns;
columns.AutoFit();

您可以插入新行并将当前日期作为标题放置,如下所示:

worksheet.Rows[1].Insert();
Excel.Range newRow = worksheet.Rows[1];
Excel.Range newCell = newRow.Cells[1];
newCell.Value = DateTime.Now.ToString("yyyy-MM-dd");

同时将此内容添加到using语句中,这样每次使用Excel对象时都不需要使用完全限定名称:

using Excel = Microsoft.Office.Interop.Excel;

另外,我已经整理了你的方法,通过添加一些using语句来减少混乱,删除一些不必要的强制转换以及对应用程序对象进行null的不必要的检查,就在它被分配之后:

using Excel = Microsoft.Office.Interop.Excel;
using System.Globalization;
using System.Threading;
using System.Data;

protected void ExportExcel(DataTable dt)
{
    if (dt == null || dt.Rows.Count == 0) return;

    var xlApp = new Excel.Application();
    //Is this used?
    CultureInfo CurrentCI = Thread.CurrentThread.CurrentCulture;

    Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

    Excel.Workbooks workbooks = xlApp.Workbooks;

    Excel.Range range

    Excel.Workbook workbook = workbooks.Add();
    Excel.Worksheet worksheet = workbook.Worksheets[1];

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

    for (var i = 0; i < dt.Columns.Count; i++)
    {
        worksheet.Cells[1, i + 1] = dt.Columns[i].ColumnName;

        range = worksheet.Cells[1, i + 1];
        range.Interior.ColorIndex = 15;
        range.Font.Bold = true;
    }

    for (var r = 0; r < dt.Rows.Count; r++)
    {
        for (var i = 0; i < dt.Columns.Count; i++)
        {
            worksheet.Cells[r + 2, i + 1] = dt.Rows[r][i].ToString();
        }

        rowRead++;

        //is this used?
        percent = ((float)(100 * rowRead)) / totalCount;
    }    

    Microsoft.Office.Interop.Excel.Range columns = worksheet.UsedRange.Columns;
    columns.AutoFit();

    worksheet.Rows[1].Insert();
    Excel.Range newRow = worksheet.Rows[1];
    Excel.Range newCell = newRow.Cells[1];
    newCell.Value = DateTime.Now.ToString("yyyy-MM-dd");

    xlApp.Visible = true;
}