开发者

How to run over DataSet and insert into Excel (not .csv)?

开发者 https://www.devze.com 2022-12-14 20:37 出处:网络
How to run over DataSet and insert into Excel (not .csv file) ? i work on C#, Visual-studio 2008, Excel 2007

How to run over DataSet and insert into Excel (not .csv file) ?

i work on C#, Visual-studio 2008, Excel 2007

tha开发者_运维技巧nk's in advance


You need to look into the Office Interop Libraries, see this example take from here.

Microsoft.Office.Interop.Excel.Application xl = 
    new Microsoft.Office.Interop.Excel.Application();

xla.Visible = true;
Workbook wb = xla.Workbooks.Add(XlSheetType.xlWorksheet);
Worksheet ws = (Worksheet)xla.ActiveSheet;
int i = 1;
int j = 1;
foreach (ListViewItem comp in lvwResults.Items)
{
    ws.Cells[i, j] = comp.Text.ToString();
    //MessageBox.Show(comp.Text.ToString());
    foreach (ListViewItem.ListViewSubItem drv in comp.SubItems)
    {
        ws.Cells[i, j] = drv.Text.ToString();
        j++;
    }
    j = ;1;
    i++;
}

This of course requrires that you have Excel installed on the machine running the program.


Here is a useful article: Reading and Writing Excel Spreadsheets Using ADO.NET C# DbProviderFactory

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号