zoukankan      html  css  js  c++  java
  • 导出EXCEL

     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位程序。

  • 相关阅读:
    养成写随笔的习惯
    脚本附加数据库
    脚本还原数据库
    C# 自定义安装包
    怕忘记了。记录一下要采购的元件1
    计划没有变化快啊
    一天三练有点累啊
    nRF24L01无线模块使用1电平转换
    好几年没参加IC公司的研讨会了
    变化
  • 原文地址:https://www.cnblogs.com/cnaspnet/p/4443321.html
Copyright © 2011-2022 走看看