protected void excelOut_Click(object sender, EventArgs e)
{
try
{
DataTable dt = ds.Tables[0];
if (dt != null && dt.Rows != null && dt.Rows.Count == 0)
{
Response.Write("<script language='javascript'>alert('没有数据!');</script>");
return;
}
string staFile = Server.MapPath("~/Manager/fileUpload");
string templetFile = staFile + @"/Bb_Divorce.xls";
string outputFile = staFile + @"/" + Perm.UserID + ".xls";
if (!File.Exists(templetFile))
{
Response.Write("<script language='javascript'>alert('不存在模板文件,请与管理员联系!');</script>");
return;
}
try
{
if (File.Exists(outputFile))
File.Delete(outputFile);
}
catch (Exception ex)
{
}
//创建一个Application对象并使其可见
//beforeTime = DateTime.Now;
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
app.Visible = false;
//afterTime = DateTime.Now;
System.Reflection.Missing miss = System.Reflection.Missing.Value;
//打开模板文件,得到WorkBook对象
Microsoft.Office.Interop.Excel.Workbook workBook = app.Workbooks.Open(templetFile, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss);
//得到WorkSheet对象
Microsoft.Office.Interop.Excel.Worksheet workSheet = (Microsoft.Office.Interop.Excel.Worksheet)workBook.Sheets.get_Item(1);
int rowCount = dt.Rows.Count; //DataTable行数
int colCount = dt.Columns.Count; //DataTable列数
//workSheet.Copy(miss, workBook.Worksheets[1]);
//workSheet = (Microsoft.Office.Interop.Excel.Worksheet)workBook.Worksheets.get_Item(1);
Microsoft.Office.Interop.Excel.Range range;
//利用二维数组批量写入
string[,] ss = new string[rowCount, colCount];
for (int j = 0; j < rowCount; j++)
{
ss[j, 0] = Convert.ToString(j + 1);
for (int k = 1; k < colCount; k++)
{
ss[j, k] = dt.Rows[j][k].ToString();
}
}
range = (Microsoft.Office.Interop.Excel.Range)workSheet.Cells[4, 1];
range = range.get_Resize(rowCount, colCount);
range.Cells.Borders.LineStyle = 1;
range.Value2 = ss;
workBook.SaveAs(outputFile, miss, miss, miss, miss, miss, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss, miss, miss);
app.DisplayAlerts = false;
workBook.Close(false, miss, miss);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workSheet);
workSheet = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook);
workBook = null;
app.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
app = null;
GC.Collect();
DownFile(outputFile, "离异人员情况表");
}
catch { }
finally
{
this.Dispose();
}
}
public void DownFile(string path, string fileName)
{
FileInfo fi = new FileInfo(path);
HttpContext.Current.Response.Clear();
// //当要下载的文件名是中文时,需加上HttpUtility.UrlEncode
HttpContext.Current.Response.Charset = "gb2312";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ".xls");
HttpContext.Current.Response.AddHeader("Content-Length", fi.Length.ToString());
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.WriteFile(fi.FullName);
HttpContext.Current.Response.End();
//HttpContext.Current.ApplicationInstance.CompleteRequest();
}
c# 导出Excel文件
最新推荐文章于 2022-02-11 09:25:55 发布