简体   繁体   中英

Extract values from a list and save it to an existing Excel sheet in c#

I have an existing Excel sheet which has headers. I get data from my server and place it in my WPF DataGrid and it looks like this:

在此处输入图片说明

On a click of a button, I need to place the values from my list to a particular sheet in my existing Excel workbook. I can actually get the values from a WINFORM DataGrid like this:

var xlApp = new Excel.Application();
Excel.Worksheet sheet = new Excel.Worksheet();
xlApp.Visible = true;
var path = @"D:\Reports\Tag_History.xlsx";
sheet = xlApp.Application.Workbooks.Open(path).Worksheets["Summary"];

var rowCount = dataGrid.Items.Count;
var rowColumn = dataGrid.Columns.Count;

for (int i = 0; i < rowCount - 1; i++)
{
    for (int j = 0; j < 7; j++)
    {
        if (dataGrid[j, i].ValueType == typeof(string))
        {
            xlsht.Cells[i + 2, j + 1] = "'" + dataGrid[j, i].Value.ToString();
        }
        else
        {
            xlsht.Cells[i + 2, j + 1] = dataGrid[j, i].Value.ToString();
        }
    }
}

but since I am trying to do this in WPF, this code does not work anymore. This is by transferring dataGrid data to an existing excel file. Since I think that transferring list to an existing excel file is better, I have to try this. This is what I have so far:

var xlApp = new Excel.Application();
Excel.Worksheet sheet = new Excel.Worksheet();
xlApp.Visible = true;
var path = @"D:\Reports\Tag_History.xlsx";
sheet = xlApp.Application.Workbooks.Open(path).Worksheets["Summary"];

var range = sheet.Range["A2", "A2"];
foreach (var item in summaryList)
{
    range.Value2 = item.TagNumber;
}

This code works but it is only updating a single cell of the excel file.

在此处输入图片说明

Can you please show me how to do this? Thank you.

Install Microsoft.Office.Interop.Excel Nuget package in your application. Right-click on your project -> "References" and choose "Manage NuGet Packages..." , then just search for Excel. Otherwise, select Tools -> Nuget Package Manager -> Package Manager Console -> then install the Excel nuget ( https://www.nuget.org/packages/Microsoft.Office.Interop.Excel/ ).

Bind the items in DataGrid and then export data to excel as like below,

private void btnExport_Click(object sender, RoutedEventArgs e)
    {
        Microsoft.Office.Interop.Excel.Application excel = null;
        Microsoft.Office.Interop.Excel.Workbook wb = null;
        object missing = Type.Missing;
        Microsoft.Office.Interop.Excel.Worksheet ws = null;
        Microsoft.Office.Interop.Excel.Range rng = null;

        // collection of DataGrid Items
        var dtExcelDataTable = ExcelTimeReport(txtFrmDte.Text, txtToDte.Text, strCondition);

        excel = new Microsoft.Office.Interop.Excel.Application();
        wb = excel.Workbooks.Add();
        ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.ActiveSheet;
        ws.Columns.AutoFit();
        ws.Columns.EntireColumn.ColumnWidth = 25;

        // Header row
        for (int Idx = 0; Idx < dtExcelDataTable.Columns.Count; Idx++)
        {
            ws.Range["A1"].Offset[0, Idx].Value = dtExcelDataTable.Columns[Idx].ColumnName;
        }

        // Data Rows
        for (int Idx = 0; Idx < dtExcelDataTable.Rows.Count; Idx++)
        {
            ws.Range["A2"].Offset[Idx].Resize[1, dtExcelDataTable.Columns.Count].Value = dtExcelDataTable.Rows[Idx].ItemArray;
        }

        excel.Visible = true;
        wb.Activate();
        wb.SaveCopyAs("excel file location");
        wb.Saved = true;            
        excel.Quit();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM