在填充到数据表之前,将所有excel列格式化为General

时间:2012-08-14 06:31:33

标签: c# asp.net .net

在填充数据集之前,是否可以将Excel工作表的所有列更改为常规类型?我不能在我的应用程序中使用Interop或任何其他第三方DLL。我正在使用OleDbDataAdapter将数据填充到数据集中。

string SourceConstr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + fileName + "';Extended Properties= 'Excel 12.0;HDR=NO;TypeGuessRows=0;ImportMixedTypes=General'";
OleDbConnection con = new OleDbConnection(SourceConstr);
string query = "Select * from [Sheet1$A9:FG100]";
OleDbDataAdapter data = new OleDbDataAdapter(query, con);
data.Fill(ds);

1 个答案:

答案 0 :(得分:0)

一种方法是获取所有列并使用

更改它们
foreach (DataColumn c in DT.Columns)
{
    iColumnCount++;
    if(iRowCount == 0)
        Worksheet.Cells[1, iColumnCount] = c.ColumnName;
    else
        Worksheet.Cells[iRowCount, iColumnCount] = c.ColumnName;

    Worksheet.Columns.AutoFit(); //Correct the width of the columns
    //THIS IS WHERE I WANT TO COLOR THE HEADERS
}


    foreach (DataRow r in DT.Rows)
    {
        iRowCount++;
        iColumnCount = 0;
        foreach (DataColumn c in DT.Columns)
        {
            iColumnCount++;
            if(iRowCount == 1)
                Worksheet.Cells[iRowCount + 1, iColumnCount] = r[c.ColumnName].ToString();
            else
                Worksheet.Cells[iRowCount, iColumnCount] = r[c.ColumnName].ToString();

            Worksheet.Columns.AutoFit(); //Correct the width of the columns
        }
    }

改变颜色或蚂蚁属性

Worksheet.Range["A1","G1"].Interior.Color = Excel.XlRgbColor.rgbDarkBlue;

Worksheet.Range [“A1”,“G1”]。name或header = Excel.XlRgbColor.rgbWhite;