C#如何导出一个Excel表格多个sheet页

2024-12-01 07:36:35
推荐回答(1个)
回答1:

string ExcelName = "设备列表" + ".xls";//导出的Excel表格名 string sql = "select * from joa_equip_class select * from joa_equip_info select * from joa_equip_parket_class select * from joa_equip_maintenance"; DataSet ds = ComData.jdataset(sql); string[][] ArrName ={ new string[] { "类别大", "类别小", "类别名", "排序", "系统时间", "备注" }, new string[] { "设备编号", "设备名称", "设备型号", "生产厂家", "生产日期", "技术状态", "存放位置", "负责人", "配属单位", "条形码", "有效期", "类别id", "存放位置id", "设备照片" }, new string[] { "类别id大", "类别id小", "存放位置", "货物号", "保管人", "备注", "排序" }, new string[] { "设备名称", "设备编号", "责任单位", "责任人", "维护保养费", "维护保养性质", "详细说明" } }; string[][] ArrDs ={ new string[] { "parent_id", "class_id", "class_name", "order_id", "systime", "remark" }, new string[] { "equip_id", "name", "model", "factory", "production_date", "state", "store", "duty_name", "unit", "bar", "useful_time", "class_id", "parket_id", "photo" }, new string[] { "parent_id", "parket_id", "class_name", "goods_nums", "person", "remark", "order_id" }, new string[] { "equip_name", "equip_id","duty_bum", "duty_name", "money", "kind", "explain" } }; string[] SheetName ={ "类别表", "设备列表" ,"存储位置表","维护保养记录表"}; Equce eq = new Equce(); eq.ToExcel(ds, ArrName, ArrDs, ExcelName, SheetName);//数据DataSet,列名,列数据名,Excel表格名,sheet页名 -------------------------------------------------------------------------------------------------------------------- //导出一个或多个数据表 public void ToExcel(DataSet ds, string[][] ArrName, string[][] ArrDs, string ExcelName, string[] Sheet_Name){AppLibrary.WriteExcel.XlsDocument doc = new AppLibrary.WriteExcel.XlsDocument(); //doc.FileName = "Report.xls"; doc.FileName = HttpUtility.UrlEncode(ExcelName, System.Text.Encoding.UTF8);//防止导出Excel乱码 string SheetName = string.Empty; int k = ds.Tables.Count;//表的个数 for (int i = 0; i < k; i++){//SheetName = "当前是SHEET" + i.ToString(); SheetName = Sheet_Name[i].ToString(); AppLibrary.WriteExcel.Worksheet sheet = doc.Workbook.Worksheets.Add(SheetName); AppLibrary.WriteExcel.Cells cells = sheet.Cells; for (int j = 1; j <= ArrName[i].Length; j++)//加入标题{if (ArrName[i][j - 1] != "") cells.Add(1, j, ArrName[i][j - 1].ToString());}int f = 1;int a = ds.Tables[i].Rows.Count; ; for (int m = 0; m < a; m++){f++;for (int n = 1; n <= ArrDs[i].Length; n++)//循环列{if (ArrDs[i][n - 1] != "") cells.Add(f, n, ds.Tables[i].Rows[m][ArrDs[i][n - 1]].ToString());}}}doc.Send();