代码如下: private void ToExcel(DataSet ds)
{
object missing = Missing.Value;
string savePath = Server.MapPath("./") + @"ExcelFile/";
DirectoryInfo upDir = new DirectoryInfo(savePath);
if (!upDir.Exists)
{
upDir.Create();
}
if (ds.Tables[0].Rows.Count != 0)
{
savePath += ds.Tables[0].Rows[0]["员工姓名"].ToString().Trim();
Excel.Application excel = new Excel.ApplicationClass();
excel.Application.Workbooks.Add(true); //Sheets xslSheets = Excel.Worksheets;
//xslSheets.get
for (int i = 1; i <= ds.Tables[0].Rows.Count; i++)
{
for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
{
excel.Cells[i,j] = ds.Tables[0].Rows[i][j].ToString();
}
} excel.Visible = false;
excel.ActiveWorkbook.SaveCopyAs(savePath);
excel.Workbooks.Close();
excel.Application.Quit();
excel.Quit();
}
}
{
object missing = Missing.Value;
string savePath = Server.MapPath("./") + @"ExcelFile/";
DirectoryInfo upDir = new DirectoryInfo(savePath);
if (!upDir.Exists)
{
upDir.Create();
}
if (ds.Tables[0].Rows.Count != 0)
{
savePath += ds.Tables[0].Rows[0]["员工姓名"].ToString().Trim();
Excel.Application excel = new Excel.ApplicationClass();
excel.Application.Workbooks.Add(true); //Sheets xslSheets = Excel.Worksheets;
//xslSheets.get
for (int i = 1; i <= ds.Tables[0].Rows.Count; i++)
{
for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
{
excel.Cells[i,j] = ds.Tables[0].Rows[i][j].ToString();
}
} excel.Visible = false;
excel.ActiveWorkbook.SaveCopyAs(savePath);
excel.Workbooks.Close();
excel.Application.Quit();
excel.Quit();
}
}
麻烦大家帮我看看是什么原因,谢谢!另外:用Response导出Excel时,能不能实现自动保存在服务器的某个目录下面?如下面代码,如果能自动保存的话,应该怎么改?FileStream file = new FileStream(this.Tem_OutPath, FileMode.Open);
byte[] buffer = new byte[(int)file.Length];
file.Read(buffer, 0, (int)file.Length);
file.Close();
string temp = string.Format("attachment;filename={0}", "ExportCSV.csv");
Response.ClearHeaders();
Response.AppendHeader("Content-disposition", temp);
Response.ContentType = "application/csv";
Response.BinaryWrite(buffer);
Response.Flush();
Response.End();谢谢!
我也遇到了! 发我邮箱[email protected] 谢谢啊
excel.ActiveWorkbook.SaveAs(strExcelFileName, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange,
Excel.XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing, Type.Missing, Type.Missing); 我这样保存倒是没错,在 2003下面.但是在 vista 下面就会有问题了,能告诉我你是怎么解决的吗.谢谢了[email protected]
for (int i = 0; i < data.Columns.Count; i++)
{
excel.Cells[2, i] = data.Columns[i].ColumnName.ToString();
}
》》》》》
for (int i = 0; i < data.Columns.Count; i++)
{
excel.Cells[2, i+1] = data.Columns[i].ColumnName.ToString();
}