private void MyExcel() { DataSet ds = new lgk.BLL.tb_bonus().GetListByUser(getWhere2());//获取所有时间段的奖金信息 if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) { string FilePath = Server.MapPath("/userfiles/");// +"\\" + ExcelFolder + "\\"; if (!Directory.Exists(FilePath)) { Directory.CreateDirectory(FilePath); } //生成列的中文对应表 Hashtable nameList = new Hashtable(); nameList.Add("UserCode", "用户名"); nameList.Add("sf", "应发奖金"); nameList.Add("BankAccountUser", "开户姓名"); nameList.Add("BankName", "开户行"); nameList.Add("BankAccount", "卡号"); nameList.Add("BankBranch", "支行名称"); //利用excel对象 DataToExcel dte = new DataToExcel(); string filename = ""; try { if (ds.Tables[0].Rows.Count > 0) { filename = dte.DataExcel(ds.Tables[0], "奖金", FilePath, nameList); } } catch { //dte.KillExcelProcess(); } if (filename != "") { string path = FilePath + filename; Response.Redirect("/userfiles/" + filename, true); } } else { ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "info", "alert('当前无数据导出!');", true); } }
private void MyExcel() { DataSet ds = GetAgentList(getWhere2());//获取信息 if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) { string FilePath = Server.MapPath("/userfiles/");// +"\\" + ExcelFolder + "\\"; if (!Directory.Exists(FilePath)) { Directory.CreateDirectory(FilePath); } //生成列的中文对应表 Hashtable nameList = new Hashtable(); nameList.Add("AgentCode", "代理中心编号"); nameList.Add("UserCode", "会员编号"); nameList.Add("TrueName", "会员姓名"); nameList.Add("LevelName", "会员级别"); nameList.Add("Quyu", "代理区域"); nameList.Add("AppliTime", "申请日期"); nameList.Add("OpenTime", "确认日期"); DataTable dt = ds.Tables[0]; dt.Columns.Remove("ID"); dt.Columns.Remove("AgentType"); dt.Columns.Remove("Flag"); dt.Columns.Remove("UserID"); dt.Columns.Remove("LevelID"); dt.Columns.Remove("AgentsID"); dt.Columns.Remove("User006"); DataTable newTable = new DataTable(); DataColumn newCol = new DataColumn(); newCol.ColumnName = "Quyu"; newTable.Columns.Add(newCol); foreach (DataColumn col in dt.Columns) { DataColumn c = new DataColumn(); c.ColumnName = col.ColumnName; newTable.Columns.Add(c); } for (int i = 0; i < dt.Rows.Count; i++) { DataRow dr = newTable.NewRow(); dr["Quyu"] = dt.Rows[i]["AgentInProvince"].ToString() + dt.Rows[i]["AgentInCity"].ToString() + dt.Rows[i]["AgentAddress"].ToString(); foreach (DataColumn ncol in dt.Columns) { dr[ncol.ColumnName] = dt.Rows[i][ncol.ColumnName]; } newTable.Rows.Add(dr); } newTable.Columns.Remove("AgentInProvince"); newTable.Columns.Remove("AgentInCity"); newTable.Columns.Remove("AgentAddress"); //利用excel对象 DataToExcel dte = new DataToExcel(); string filename = ""; try { if (ds.Tables[0].Rows.Count > 0) { filename = dte.DataExcel(newTable, "已开通代理中心", FilePath, nameList); } } catch { //dte.KillExcelProcess(); } if (filename != "") { string path = FilePath + filename; Response.Redirect("/userfiles/" + filename, true); } } else { ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "info", "alert('当前无数据导出!');", true); } }
public static void ExportToExcel(DataSet ds, Hashtable displaycol_nameList, string otherInfo) { common_file.common_app.get_czsj(); if (ds != null && ds.Tables[0].Rows.Count > 0) { string filePath = ""; string fileName = ""; string get_fileName = ""; Hashtable nameList = new Hashtable(); nameList = displaycol_nameList; SaveFileDialog saveFileDialog1; FolderBrowserDialog folderBrowserDialog1; //利用excel对象 DataToExcel dte = new DataToExcel(); try { common_file.common_app.get_czsj(); if (ds.Tables[0].Rows.Count > 0) { common_file.common_app.get_czsj(); saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Filter = "Excel files (*.xls)|*.xls"; folderBrowserDialog1 = new FolderBrowserDialog(); if (defaulPath.Trim() != "") { folderBrowserDialog1.SelectedPath = defaulPath; } if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) { common_file.common_app.get_czsj(); filePath = folderBrowserDialog1.SelectedPath + "\\"; defaulPath = folderBrowserDialog1.SelectedPath; get_fileName = dte.DataExcel(ds.Tables[0], otherInfo, filePath, nameList).Trim(); if (get_fileName != "") { fileName = filePath + get_fileName; } else { return; } } } } catch { dte.KillExcelProcess(); } if (get_fileName.Trim() != "") { common_file.common_app.get_czsj(); common_file.common_app.Message_box_show(common_file.common_app.message_title, "导出成功!"); //Response.Redirect((ExcelFolder+"\\"+filename,true); //StreamWriter aWriter = new StreamWriter(fileName); aWriter.Write(fileName); aWriter.Close(); } } Cursor.Current = Cursors.Default; }