using ExcelLibrary.SpreadSheet;
using System.IO;
private string ExportExcel(DataSet ds, ref string msg)
{
string path = null;
try
{
System.Data.DataTable dt = ds.Tables[0];
Workbook workbook = new Workbook();
Worksheet worksheet = new Worksheet("表1");
int columnsCount = dt.Columns.Count;
for (int i =0; i < columnsCount-2; i++)
{
worksheet.Cells[0, i] = new Cell(dt.Columns[i].ColumnName, "@");
}
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < columnsCount-2; j++)
worksheet.Cells[i + 1, j] = new Cell(dt.Rows[i][j] == DBNull.Value ? "" : dt.Rows[i][j].ToString(), "@");
}
workbook.Worksheets.Add(worksheet);
string path1="~/Lgr/ExportedFiles" + "\\" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
path = System.Web.HttpContext.Current.Server.MapPath(path1);
workbook.Save(path);
OutputFile(".xls",path);
}
catch (Exception ex)
{
path = null;
msg = ex.Message;
}
return path;
}
private void OutputFile(string contentType, string generatedFileName)
{
FileInfo file = new FileInfo(generatedFileName);
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpContext.Current.Server.UrlEncode(file.Name));
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = contentType;
Response.WriteFile(file.FullName);
//Response.End();
}