Устанавливаем o2007pia.msi, в проекте, через AddReference добавляем COM-объект "Microsoft Excel 12 Object Library":
using Microsoft.Office.Interop.Excel;
...
private void btnSaveXls_Click(object sender, EventArgs e)
{
// creating Excel Application
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
// creating new WorkBook within Excel application
Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
// creating new Excelsheet in workbook
Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
// see the excel sheet behind the program
app.Visible = true;
// store its reference to worksheet
worksheet = (Worksheet)workbook.ActiveSheet;
// changing the name of active sheet
worksheet.Name = "Exported from gridview";
// storing header part in Excel
for (int i = 1; i < dgvV.Columns.Count + 1; i++)
{
worksheet.Cells[1, i] = dgvV.Columns[i - 1].HeaderText;
}
worksheet.get_Range("A1").AutoFit();
worksheet.get_Range("B1").AutoFit();
worksheet.get_Range("C1").AutoFit();
// storing Each row and column value to excel sheet
for (int i = 0; i < dgvV.Rows.Count - 1; i++)
{
for (int j = 0; j < dgvV.Columns.Count; j++)
{
worksheet.Cells[i + 2, j + 1] = dgvV.Rows[i].Cells[j].Value.ToString();
}
}
// save the application
workbook.SaveAs("c:\\output.xlsx", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
// Exit from the application
app.Quit();
}
using Microsoft.Office.Interop.Excel;
...
private void btnSaveXls_Click(object sender, EventArgs e)
{
// creating Excel Application
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
// creating new WorkBook within Excel application
Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
// creating new Excelsheet in workbook
Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
// see the excel sheet behind the program
app.Visible = true;
// store its reference to worksheet
worksheet = (Worksheet)workbook.ActiveSheet;
// changing the name of active sheet
worksheet.Name = "Exported from gridview";
// storing header part in Excel
for (int i = 1; i < dgvV.Columns.Count + 1; i++)
{
worksheet.Cells[1, i] = dgvV.Columns[i - 1].HeaderText;
}
worksheet.get_Range("A1").AutoFit();
worksheet.get_Range("B1").AutoFit();
worksheet.get_Range("C1").AutoFit();
// storing Each row and column value to excel sheet
for (int i = 0; i < dgvV.Rows.Count - 1; i++)
{
for (int j = 0; j < dgvV.Columns.Count; j++)
{
worksheet.Cells[i + 2, j + 1] = dgvV.Rows[i].Cells[j].Value.ToString();
}
}
// save the application
workbook.SaveAs("c:\\output.xlsx", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
// Exit from the application
app.Quit();
}
Комментариев нет:
Отправить комментарий