0

我正在使用 c# 编程语言将数据集导出到现有的 Excel 工作表。因此,我需要指定要插入数据的确切列和行(列 G,行 3)。请注意,数据集只有 1 列。我已经提到了我使用的编码。但它不会将数据集传递到我需要的位置。请帮忙

dt = compareDataTables(Oracle, MIT);
        dataGridView1.DataSource = dt;  

        //inserting to Excel file
        Excel.Application xlApp;
        Excel.Workbook xlWorkBook;
        Excel.Worksheet xlWorkSheet;
        object misValue = System.Reflection.Missing.Value;
        xlApp = new Excel.Application();
        xlWorkBook = xlApp.Workbooks.Open(textBox1.Text, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue);
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

        int i = 0;
        int j = 0;

        for (i = 0; i <= dataGridView1.RowCount - 1; i++)
        {
            for (j = 0; j <= dataGridView1.ColumnCount - 1; j++)
            {
                DataGridViewCell cell = dataGridView1[j, i];
                xlWorkSheet.Cells["3", "G"] = cell.Value;
            }
        }
        xlWorkBook.Save();
        xlWorkBook.Close(true, misValue, misValue);
        xlApp.Quit();

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

1 回答 1

0

为什么 xlWorkSheet.Cells["3", "G"] 是硬编码的?,它每次都重写数据

于 2012-10-16T05:45:08.973 回答