'How to insert data into row 17 excel file c#
I'm trying to make a button that exports the data of my database but it loads the excel but doesn't insert the data into it.
this is the code that I have right now: https://pastebin.com/WaDgya9N
I already tried
for (int i = 0; i < dataGridViewOil.Rows.Count - 1; i++)
{
for (int j = 0; j < dataGridViewOil.Columns.Count; j++)
{
xlWorkSheet.Cells[i + 17, j + 1] =
dataGridViewOil.Rows[i].Cells[j].Value.ToString();
}
}
and
for (int i = 1; i < dataGridViewOil.Rows.Count; i++)
{
for (int j = 0; j < dataGridViewOil.Columns.Count; j++)
{
xlWorkSheet.Cells[i + 17, j + 1] =
dataGridViewOil.Rows[i].Cells[j].Value.ToString();
}
}
export code
private void buttonExportar_Click_1(object sender, EventArgs e)
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(
@"C:\Users\tabletp\Desktop\Donativos\Donativos\bin\DOC 04 02 - OIL - Open Issue List.xlsm"
,0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows
, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
for (int i = 1; i <= dataGridViewOil.Columns.Count; i++)
{
xlWorkSheet.Cells[1, i].HorizontalAlignment =
Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
xlWorkSheet.Cells[1, i].Font.Bold = true;
}
for (int i = 1; i < dataGridViewOil.Rows.Count; i++)
{
for (int j = 17; j < dataGridViewOil.Columns.Count; j++)
{
xlWorkSheet.Cells[i + 17, j + 1] =
dataGridViewOil.Rows[i].Cells[j].Value.ToString();
}
}
xlApp.Visible = true;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
