标签:
public static class OutputExcel { private static string outPath = System.Web.HttpContext.Current.Server.MapPath("~"); private static string outPathHadTrim = System.Web.HttpContext.Current.Server.MapPath("~").TrimEnd(new char[] { ‘/‘, ‘\\‘ }); /// <summary> /// 导出Table中的数据到Excel文件(扩展名为xlsx) /// </summary> /// <param name="Response"></param> /// <param name="writeTable">包含导出数据的Table</param> /// <param name="colHeads">列表中各列的标题</param> /// <param name="colNames">writeTable中要导出列的列名</param> /// <param name="title">页面名称</param> /// <param name="filename"></param> /// <returns>导出Excel是否成功</returns> public static bool exportExcelStream(System.Web.HttpResponse Response, System.Data.DataTable writeTable, string[] colHeads, string[] colNames, string title, string strfilename, out string outputAllpathfile) { //return export_ToExcel(writeTable, colHeads, colNames, title, strfilename, false, null, out outputAllpathfile); return Output_oledbExcel.Export_Excel(writeTable, colHeads, colNames, strfilename, null, out outputAllpathfile); } public static bool exportExcelStream(System.Web.HttpResponse Response, System.Data.DataTable writeTable, string[] colHeads, string[] colNames, string title, string strfilename, bool hasFirstNoCol, string[] dateFormatCols, out string outputAllpathfile) { return Output_oledbExcel.Export_Excel(writeTable, colHeads, colNames, strfilename, dateFormatCols, out outputAllpathfile); } }
public static class Output_oledbExcel { private static string outPath = System.Web.HttpContext.Current.Server.MapPath("~"); public static bool Export_Excel(System.Data.DataTable writeTable, string[] colHeads, string[] colNames, string strfilename, string[] dateFormatCols, out string outputAllpathfile) { string t_exportpath = DateTime.Now.ToString("yyyyMMdd"); long tickets = System.DateTime.Now.Ticks; tickets = tickets % 1000000; string filename = strfilename + tickets; outputAllpathfile = @"/Log/" + t_exportpath + @"/" + filename + ".xlsx"; if (writeTable == null) { return false; } if (string.IsNullOrEmpty(outPath)) { return false; } string outputPath = outPath; if (outputPath != null && outputPath.Length > 0) { outputPath = outputPath + @"Log\" + t_exportpath + @"\"; } else { //"OutputPath could not be copyed." return false; } if (!System.IO.Directory.Exists(outputPath)) { System.IO.Directory.CreateDirectory(outputPath); } string outFileName = filename + ".xlsx"; string outPathFileName = outputPath + outFileName; List<string> colsnameList = new List<string>(); List<string> colHeadsList = new List<string>(); List<int> dateFormatColsList = new List<int>(); bool date_Format = false; if (dateFormatCols != null && dateFormatCols.Length > 0) { date_Format = true; } int intColindex = 0; StringBuilder strfield = new StringBuilder(); for (int intcol = 0; intcol < colNames.Length; intcol++) { if (writeTable.Columns.Contains(colNames[intcol]) && intcol < colHeads.Length) { colHeadsList.Add(colHeads[intcol]); colsnameList.Add(colNames[intcol]); if (date_Format && dateFormatCols.Contains(colNames[intcol])) { dateFormatColsList.Add(intColindex); } strfield.Append("[" + colHeads[intcol] + "]"); strfield.Append(","); intColindex++; } } if (strfield.Length > 1) { strfield.Remove(strfield.Length - 1, 1); } //////excel 2003格式 ////string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + outPathFileName + ";Extended Properties=Excel 8.0;"; ////Excel 2007格式 string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + outPathFileName + ";Extended Properties=Excel 12.0 Xml;"; try { using (OleDbConnection con = new OleDbConnection(connString)) { con.Open(); StringBuilder strSQL = new StringBuilder(); strSQL.Append("CREATE TABLE ").Append("[Sheet1]"); strSQL.Append("("); for (int coli = 0; coli < colHeadsList.Count; coli++) { strSQL.Append("[" + colHeadsList[coli] + "] text,"); } strSQL = strSQL.Remove(strSQL.Length - 1, 1); strSQL.Append(")"); OleDbCommand cmd = new OleDbCommand(strSQL.ToString(), con); cmd.ExecuteNonQuery(); //for (int KKK = 0; KKK < 30; KKK++) for (int i = 0; i < writeTable.Rows.Count; i++) { strSQL.Clear(); StringBuilder strvalue = new StringBuilder(); for (int j = 0; j < colHeadsList.Count; j++) { if (date_Format && dateFormatColsList.Contains(j)) { strvalue.Append("‘" + ReadVal.DateStr(writeTable.Rows[i], colsnameList[j]) + "‘"); } else { strvalue.Append("‘" + writeTable.Rows[i][colsnameList[j]].ToString() + "‘"); } //if (j < colHeadsList.Count - 1) //{ strvalue.Append(","); //} } if (strvalue.Length > 1) { strvalue.Remove(strvalue.Length - 1, 1); } cmd.CommandText = strSQL.Append("insert into [Sheet1]( ") .Append(strfield.ToString()) .Append(") values (").Append(strvalue).Append(")").ToString(); cmd.ExecuteNonQuery(); } con.Close(); return true; } } catch (Exception ex) { } return false; } }
调用:
public void getExcel() {
//根据查询条件返回DataTable
DataTable dtchage = 查询返回的DataTable;
string outpathfile = ""; if (OutputExcel.exportExcelStream(System.Web.HttpContext.Current.Response, dtchage, new string[] { "Legal", "L2", "部门", "日期", "是否工作日", "员工编号", "姓名", "工时类型", "项目公司", "项目部门", "项目编号", "项目名称", "工时", "状态", "PM", "PL", "填写日期", "确认人", "确认时间 ", "Laborcost", "Source" }, new string[] { "LegalEntity", "EmployeeL2", "EmployeeDept", "WorkDate", "IsWorkDay", "EmployeeNo", "EmployeeName", "WorkType", "ProjectLegal", "ProjectDepartment", "ProjectCode", "ProjectName", "Manhour", "TicketState", "PM", "PL", "FillInTime", "Reviewer", "ReviewDate", "LaborCost", "DataSource" }, "部门项目-工时查看", "DepartmentProjectTicket", false, new string[] { "WorkDate", "FillInTime" }, out outpathfile )) { //导出成功 } }
标签:
原文地址:http://www.cnblogs.com/caozhiping/p/4797732.html