0
我在寡婦應用數據GridView和我添加按鈕導出數據到Excel工作表,但這些出現錯誤當出口數據網格視圖練成片
Cannot implicitly convert type 'object' to 'Microsoft.Office.Interop.Excel._Worksheet'. An explicit conversion exists (are you missing a cast?) here (worksheet = workbook.Sheets["Sheet1"];)
和此錯誤apeared(無重載方法「另存爲」需要 '11' 參數)這裏
workbook.SaveAs("c:\\output.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing)
,但此錯誤apeared
我的代碼:
Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD)
private void button1_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;
// get the reference of first sheet. By default its name is Sheet1.
// store its reference to worksheet
worksheet = workbook.Sheets["Sheet1"];
worksheet = workbook.ActiveSheet;
// changing the name of active sheet
worksheet.Name = "Exported from gridview";
// storing header part in Excel
for (int i = 1; i < DGData.Columns.Count + 1; i++)
{
worksheet.Cells[1, i] = DGData.Columns[i - 1].HeaderText;
}
// storing Each row and column value to excel sheet
for (int i = 0; i < DGData.Rows.Count - 1; i++)
{
for (int j = 0; j < DGData.Columns.Count; j++)
{
worksheet.Cells[i + 2, j + 1] = DGData.Rows[i].Cells[j].Value.ToString();
}
}
// save the application
workbook.SaveAs("c:\\output.xls", 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();
// storing Each row and column value to excel sheet
for (int i = 0; i < DGData.Rows.Count - 1; i++)
{
for (int j = 0; j < DGData.Columns.Count; j++)
{
worksheet.Cells[i + 2, j + 1] = DGData.Rows[i].Cells[j].Value.ToString();
}
}
}
根據Microsoft的建議,出於法律和線程原因,您不應該使用Office Interop來從Web應用程序生成Excel。改用像http://epplus.codeplex.com/這樣的管理方式。 – jimmystormig 2011-03-03 10:48:39