码迷,mamicode.com
首页 > 其他好文 > 详细

导出EXCEL

时间:2015-04-21 09:26:58      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

 public static void ChangeExcel(string strcodecom, string stryear, string strmonther, string strdays, string strtype, string struserid)
        {
            string Template = HttpContext.Current.Request.MapPath("~/template/listexcel.xls");
            string FileTemp = HttpContext.Current.Request.MapPath("~/template/" + System.Guid.NewGuid().ToString("N").ToUpper()) + ".xls";
            File.Copy(Template, FileTemp);
            string ConString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Extended Properties=Excel 8.0;" + "Data Source=" + FileTemp;
            string SentenceExcel = String.Format("SELECT * FROM [{0}$]""Sheet1");
            OleDbDataAdapter AdapterClass = new OleDbDataAdapter(SentenceExcel, ConString);
            OleDbCommandBuilder CommandBuilderClass = new OleDbCommandBuilder(AdapterClass);
            CommandBuilderClass.QuotePrefix = "[";
            CommandBuilderClass.QuoteSuffix = "]";
            DataSet DataSetClass = new DataSet();
            AdapterClass.Fill(DataSetClass, "TableName");
            DataTable exceldt = DataSetClass.Tables[0];

            WebClass.CodeList tmp = new WebClass.CodeList();
            List <Models.CodeInfo> list= tmp.CodeListExcel(strcodecom, stryear, strmonther, strdays, strtype, struserid);

            if (list.Count == 0)
            {
                HttpContext.Current.Response.Write("没记录");
                HttpContext.Current.Response.Flush();
                HttpContext.Current.Response.End();
            }
            foreach (Models.CodeInfo info in list)
            {
                DataRow NewRow = exceldt.NewRow();
                NewRow["条码"] = info.Codename;
                NewRow["录入者"] = info.Username;
                NewRow["操作时间"] = info.Stime;

                exceldt.Rows.Add(NewRow);
            }
            DataTable DataTableChange = exceldt.GetChanges();
            AdapterClass.Update(DataTableChange);
            //输出文件
            HttpResponse Response = HttpContext.Current.Response;
            Response.AppendHeader("Content-Disposition""attachment;filename=" + HttpUtility.UrlEncode("ListCodeExcel") + ".xls");
            Response.ContentType = "application/ms-excel";
            Response.WriteFile(FileTemp);
            Response.Flush();
            File.Delete(FileTemp);
        }

II7.5要改兼容32位程序。

 

 

导出EXCEL

标签:

原文地址:http://www.cnblogs.com/cnaspnet/p/4443321.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!