public void SaveDetailToExcel(DataSet ds, string filePath)
{
try
{
GC.Collect();
Application app;
Worksheet wSheet = null;
Workbook wBook; app = new ApplicationClass(); app.Visible = false;
wBook = app.Workbooks.Add(true); int t = ds.Tables.Count; for (int sheetIndex = 0; sheetIndex < t; sheetIndex++)
{
wSheet = (Worksheet)wBook.Worksheets.Add(Type.Missing, Type.Missing, 1, Type.Missing);
wSheet.Name = ds.Tables[sheetIndex].TableName; //赋值
if (ds.Tables[sheetIndex].Rows.Count > 0)
{
int row = 0;
row = ds.Tables[sheetIndex].Rows.Count;
int col = ds.Tables[sheetIndex].Columns.Count; for (int i = 3; i <= row + 2; i++)
{
for (int j = 2; j < col; j++)
{
string str = ds.Tables[sheetIndex].Rows[i - 3][j].ToString();
wSheet.Cells[i + 1, j - 1] = str;
}
}
} wSheet.Cells[1, 1] = "表"; //设置禁止弹出保存和覆盖的询问提示框
app.DisplayAlerts = false;
app.AlertBeforeOverwriting = false;
} wBook.Save();
app.Save(filePath); wBook.Close(false, null, null);
app.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(wBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wSheet); wBook = null;
app = null;
wSheet = null;
GC.Collect();
}
catch (Exception ex)
{
throw ex;
}
finally
{
}
}我用这个把数据库里面类容保存了一个execl在本地,为什么发给别人,都打开不了,一打开就提示错误,只能在我本地打开
如果您正试图从"文件"菜单上最近使用的文档列表中打开文件,请确认文件未被重命名,移动或删除.我机子上面装的是office2007,别人好像都是2003的,但按上面生成的文件是.XLS的execl,应该2003都打的开的吧
你装个最早的.NETOFFICE支持吧..