c# 导出Excel文件


        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();


        }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值