模板导出

 string FilePath = AppDomain.CurrentDomain.BaseDirectory + "DownLoad/";
 public void Export()
 {
   string strLastFileName = '123111.xlsx'
   string strNextFileName = 'New.xlsx'
   System.IO.FileInfo newfile = new FileInfo(FilePath + strLastFileName);
   ExcelPackage excel = new ExcelPackage(newfile);
   ExcelWorksheet sheet = excel.Workbook.Worksheets["Initial"];
   DataTable dt= SQLHelper.ExecuteDataAdapter(SQLHelper.connectionString, CommandType.Text, "select * from Table1");
   for (int i  = 0;i<  dt.Rows.Count; i++)
   {
     DataRow row = dt.Rows[i];
     sheet.Cells["B" + (9 + i)].Value = row["BB"].ToString();
     sheet.Cells["C" + (9 + i)].Value = row["CC"].ToString();
     sheet.Cells["D" + (9 + i)].Value = row["DD"].ToString();
     sheet.Cells["E" + (9 + i)].Value = row["EE"].ToString();
     sheet.Cells["F" + (9 + i)].Value = row["FF"].ToString(); 
     sheet.Cells["G" + (9 + i)].Value = row["GG"].ToString();
     sheet.Cells["H" + (9 + i)].Value = row["HH"].ToString();
     //SaveAs之后即可在此路径下找到此文件
     excel.SaveAs(new FileInfo(FilePath + strNextFileName));
     sheet.Dispose();
     excel.Dispose();
     //Response.Redirect("New.xlsx路径");需要弹出保存的 放开这句话即可
   }
}
 

你可能感兴趣的:(模板导出)