how to insert data into excel sheet using C#?

2019-02-19 02:10发布

i want to create new excel sheet and insert data into it, from string array. i used the following code to create, now i want to insert data.

    Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;
            xlApp = new Excel.Application();
            //xlApp = new Excel._Application.ApplicationClass();
            xlWorkBook = xlApp.Workbooks.Add(misValue);

            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            xlWorkSheet.Cells[1, 1] = "http://csharp.net-informations.com";

            xlWorkBook.SaveAs("csharp-Excel.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);
}
    private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        }

标签: c# excel
3条回答
我想做一个坏孩纸
2楼-- · 2019-02-19 02:55

Calling COM methods is very slow. If you plan to insert a lot of data, I recommend you fetch the entire range you need, set all values, and add it back:

var range = xlWorkSheet.get_Range[firstCell, lastCell]; // or string range
var data = (object[,])range.get_Value(XlRangeValueDataType.xlRangeValueDefault);
// Set data here. Remember it's 1 based index, and row (y component) first
range.set_Value(data);
查看更多
啃猪蹄的小仙女
3楼-- · 2019-02-19 02:56
private void ExportToExcel(string fileName, DataGridView dgv) //Exports the given dataGridView to Excel with the given fileName
        {
            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;

            xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            int i = 0;
            int j = 0;

            DataTable dtExcelTable = GetDataTableForExcel(dgv);

            for (i = 0; i < dtExcelTable.Columns.Count; i++)
            {
                xlWorkSheet.Cells[1, i + 1] = dtExcelTable.Columns[i].ColumnName;
            }

            for (i = 0; i < dtExcelTable.Rows.Count; i++)
            {
                for (j = 0; j < dtExcelTable.Columns.Count; j++)
                {
                    xlWorkSheet.Cells[i + 2, j + 1] = dtExcelTable.Rows[i][j];
                }
            }

            try
            {
                xlWorkBook.SaveAs(fileName + ".xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
                xlWorkBook.Close(true, misValue, misValue);
                xlApp.Quit();

                releaseObject(xlWorkSheet);
                releaseObject(xlWorkBook);
                releaseObject(xlApp);

                MessageBox.Show("Excel dosyanız C:\\" + fileName + ".xls uzantısında yaratıldı.");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Bir sorun oluştu, tekrar deneyiniz. Hata: " + ex.Message);
            }

        }
查看更多
Jack Green
4楼-- · 2020-07-28 06:15

I use Zet Excel ! and it does everything I need with regards to parsing MS Excel files.It's Support for Office Open XML (OOXML) formats

查看更多
登录 后发表回答