1
我有一個列,A列。在列A有2個子列,A1和A2。如何使用C#將表格導出爲Excel文件?如何將包含子列的表導出到Excel文件?
我有一個列,A列。在列A有2個子列,A1和A2。如何使用C#將表格導出爲Excel文件?如何將包含子列的表導出到Excel文件?
如果你談論的是數據網格視圖中,可以這樣做:
public void ExportToExecl(DataGridView dg, string filename)
{
// 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;
// get the reference of first sheet. By default its name is Sheet1.
// store its reference to worksheet
try
{
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets["Sheet1"];
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.ActiveSheet;
// changing the name of active sheet
worksheet.Name = "Exported from History Parsing";
// storing header part in Excel
for (int i = 1; i < dg.Columns.Count + 1; i++)
{
worksheet.Cells[1, i] = dg.Columns[i - 1].HeaderText;
worksheet.Cells[i + 2, j + 1].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Gray);
}
// storing Each row and column value to excel sheet
for (int i = 0; i < dg.Rows.Count - 1; i++)
{
for (int j = 0; j < dg.Columns.Count; j++)
{
worksheet.Cells[i + 2, j + 1] = dg.Rows[i].Cells[j].Value.ToString();
worksheet.Cells[i + 2, j + 1].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Gray);
}
}
// save the application
workbook.SaveAs(filename, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
MessageBox.Show("Your excel file was created successfully");
}
catch (System.Exception ex)
{
}
finally
{
app.Quit();
workbook = null;
app = null;
}
}
你也可以閱讀有關出口DGV這裏擅長:
http://www.codeproject.com/Articles/28269/Exporting-a-DataGridView-to-an-Excel-PDF-image-fil
這裏:
http://www.codeproject.com/Articles/43400/Generalized-DataGridView-Export-to-Excel-with-Them
我們需要更多有關該表的信息。它是一個可數據對象嗎?你在使用數據庫嗎?怎麼了 – Jonesopolis
是什麼表? HTML表格,表格對象?更加詳細一些。 – Nisha