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