4

是否可以在填充数据集之前将 Excel 工作表的所有列更改为常规类型?我不能在我的应用程序中使用 Interop 或任何其他 3rd 方 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);
4

1 回答 1

-1

一种方法是获取所有列并更改它们

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 or header = Excel.XlRgbColor.rgbWhite;

于 2012-10-23T10:27:32.400 回答