我正在尝试将我网站中的数据集导出为excel。 代码在本地机器上工作正常,但是当我部署相同以获胜时。服务器即将收到以下错误:
我正在使用以下代码:
DataSet ds = new DataSet("New_DataSet");
DataTable dt = new DataTable("New_DataTable");
string constr = ConfigurationSettings.AppSettings["ConnectionInfo"];
SqlConnection con = new SqlConnection(constr);
con.Open();
string sql = "SELECT * FROM Table1";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataAdapter adptr = new SqlDataAdapter();
adptr.SelectCommand = cmd;
adptr.Fill(dt);
con.Close();
ds.Tables.Add(dt);
MyExcel.Application oXL;
MyExcel.Workbook oWB;
MyExcel.Worksheet oSheet;
MyExcel.Range oRange;
// Worksheets sheet = excelApp.Worksheets; //< - 重要的部分 //工作表sheet = sheets.Open(...);
oXL = new MyExcel.Application();
oXL.Visible = true;
oXL.DisplayAlerts = false;
oWB = oXL.Workbooks.Add(System.Type.Missing);
//oWB = oXL.Workbooks.Add(System.Type.Missing);
oSheet = (MyExcel.Worksheet)oWB.ActiveSheet;
oSheet.Name = "Customers";
int rowCount = 1;
foreach (DataRow dr in dt.Rows)
{
rowCount += 1;
for (int i = 1; i < dt.Columns.Count + 1; i++)
{
// Add the header the first time through
if (rowCount == 2)
{
oSheet.Cells[1, i] = dt.Columns[i - 1].ColumnName;
}
oSheet.Cells[rowCount, i] = dr[i - 1].ToString();
}
}
// Resize the columns
oRange = oSheet.get_Range(oSheet.Cells[1, 1],
oSheet.Cells[rowCount, dt.Columns.Count]);
oRange.EntireColumn.AutoFit();
// Save the sheet and close
oWB.SaveAs("test.xlsx", MyExcel.XlFileFormat.xlWorkbookNormal, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
MyExcel.XlSaveAsAccessMode.xlExclusive, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
oSheet = null;
oRange = null;
oWB = null;
oXL = null;
oWB.Close(false, System.Type.Missing, System.Type.Missing);
Marshal.FinalReleaseComObject(oWB);
Marshal.FinalReleaseComObject(oXL);
oXL.Quit();
Marshal.ReleaseComObject(oSheet);
Marshal.ReleaseComObject(oSheet);
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
我没有得到任何解决方法。有人可以帮忙吗?
答案 0 :(得分:2)
如果您已检查过您有足够的可用空间,则唯一的另一个问题是权限。
希望您觉得此链接很有用:
http://www.codeproject.com/Questions/87128/Unable-to-write-data-runtime-in-Excel-app-using-c