How to insert (bind) DataTable to Excel Workbook (

2019-09-16 13:27发布

I have WCF Service that returns data as DataTable type. I want to insert this data into Excel workbook in the Workbook_Startup event.

What is the best way to do it?

(VS .NET 3.5 Excel 2010 Workbook Project)


WCF Service code:

public DataTable GetQuarterTargetAchievement()
{
    var dt = new DataTable("TargetAchievement");

    using (var conn = new SqlConnection(GetConnectionString()))
    {
        using (var da = new SqlDataAdapter("SELECT fld1, fld2, ... , fldN FROM dbo.ReportTable; ", conn))
        {
            da.Fill(dt);
        }
    }

    return dt;
}

Client code:

var dt = proxy.GetQuarterTargetAchievement();

4条回答
干净又极端
2楼-- · 2019-09-16 13:30

Final solution:

var dt = proxy.GetQuarterTargetAchievement();

int
    column = 0,
    row = 0;

foreach (DataColumn col in dt.Columns)
{
    this.Application.Cells[1, ++column] = col.ColumnName;
}

foreach (DataRow r in dt.Rows)
{
    row++;
    column = 0;
    foreach (DataColumn c in dt.Columns)
    {
        this.Application.Cells[row + 1, ++column] = r[c.ColumnName];
    }
}
查看更多
够拽才男人
3楼-- · 2019-09-16 13:36

Try this example this should be enough to get you started in your using section at the top add this

using Microsoft.Office.Interop.Excel; 
using System.Runtime.InteropServices;

you may also have to add the Reference at the Project level as well make sure you order the list when you select Reference--> add--> and select the correct version of Microsoft Office Interop assembly based on the installed version you have

probably ver 12 or 14 I am guessing..

private static void Excel_FromDataTable(DataTable dt)
{
    // Create an Excel object and add workbook...
    Excel.ApplicationClass excel = new Excel.ApplicationClass();
    Excel.Workbook workbook = excel.Application.Workbooks.Add(true); // true for object template???

    // Add column headings...
    int iCol = 0;
    foreach (DataColumn c in dt.Columns)
    {
        iCol++;
        excel.Cells[1, iCol] = c.ColumnName;
    }
    // for each row of data...
    int iRow = 0;
    foreach (DataRow r in dt.Rows)
    {
        iRow++;

        // add each row's cell data...
        iCol = 0;
        foreach (DataColumn c in dt.Columns)
        {
            iCol++;
            excel.Cells[iRow + 1, iCol] = r[c.ColumnName];
        }
    }

    // Global missing reference for objects we are not defining...
    object missing = System.Reflection.Missing.Value;

    // If wanting to Save the workbook...
    workbook.SaveAs("MyExcelWorkBook.xls",
    Excel.XlFileFormat.xlXMLSpreadsheet, missing, missing,
    false, false, Excel.XlSaveAsAccessMode.xlNoChange,
    missing, missing, missing, missing, missing);

    // If wanting to make Excel visible and activate the worksheet...
    excel.Visible = true;
    Excel.Worksheet worksheet = (Excel.Worksheet)excel.ActiveSheet;
    ((Excel._Worksheet)worksheet).Activate();

    // If wanting excel to shutdown...
    ((Excel._Application)excel).Quit();
}
查看更多
We Are One
4楼-- · 2019-09-16 13:40

There is a open soure library called EPPlus, I found one tutorial on that. It could be helpful for you.

DataTable to Excel

查看更多
欢心
5楼-- · 2019-09-16 13:45

Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD))

Microsoft.Office.Interop.Excel.Workbook workbook = 
    excel.Application.Workbooks.Add(true); // true for object template???
查看更多
登录 后发表回答