c#代码:
首先要引用类库:
using System.Collections.Specialized;
using System.IO;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
1 public FileResult toexcel() 2 { 3 string salename = Request.QueryString["salename"].ToString(); 4 int seltype = int.Parse(Request.QueryString["seltype"].ToString()); 5 int sid = int.Parse(Request.QueryString["sid"].ToString()); 6 string startime = Request.QueryString["startime"].ToString(); 7 string endtime = Request.QueryString["endtime"].ToString(); 8 9 10 List<yatiku_ordersExcel> list = new yatiku_orderBLL().GetOrderListToExcel(seltype, sid, startime, endtime); 11 List<string> datelist = list.Select(p => p.orderdate.ToString()).Distinct().ToList(); 12 13 NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook(); 14 //ICellStyle style = book.CreateCellStyle(); 15 16 NPOI.SS.UserModel.ISheet sheet1 = book.CreateSheet("明细"); 17 sheet1.SetColumnWidth(0, 20 * 256); 18 sheet1.SetColumnWidth(1, 40 * 256); 19 sheet1.SetColumnWidth(7, 20 * 256); 20 sheet1.SetColumnWidth(8, 20 * 256); 21 22 NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow(0); 23 ICell cell = row1.CreateCell(0); 24 cell.CellStyle.Alignment = HorizontalAlignment.Center; 25 cell.SetCellValue(salename + "[" + startime + "至" + endtime + "]" + "数据报表"); 26 //sheet1.CreateRow(0).Height = 200 * 20; 27 28 sheet1.AddMergedRegion(new CellRangeAddress(0, 3, 0, 8)); 29 NPOI.SS.UserModel.IRow row4 = sheet1.CreateRow(4); 30 row4.CreateCell(2).SetCellValue("总数量"); 31 row4.CreateCell(3).SetCellValue("总单价"); 32 row4.CreateCell(4).SetCellValue("总运费"); 33 row4.CreateCell(5).SetCellValue("总计"); 34 35 //给sheet1添加第一行的头部标题 36 NPOI.SS.UserModel.IRow row2 = sheet1.CreateRow(7); 37 row2.CreateCell(0).CellStyle.VerticalAlignment = VerticalAlignment.Center; 38 row2.CreateCell(0).SetCellValue("时间"); 39 row2.CreateCell(1).SetCellValue("科目"); 40 row2.CreateCell(2).SetCellValue("数量"); 41 row2.CreateCell(3).SetCellValue("单价"); 42 row2.CreateCell(4).SetCellValue("运费"); 43 row2.CreateCell(5).SetCellValue("小计"); 44 row2.CreateCell(6).SetCellValue("备注"); 45 row2.CreateCell(7).SetCellValue("订单号"); 46 row2.CreateCell(8).SetCellValue("快递单号"); 47 int starRow = 8; //记录每个时间段行数 48 int endrowsadd = 7;//合并到第几行 49 //endrowsadd = ol.Count + starowsadd;//获取总行数 50 int number = 0; 51 double pricesum = 0; 52 double costsum = 0; 53 double paysum = 0;//总价 54 int j = 8; 55 List<yatiku_order_itemModels> listresult = new List<yatiku_order_itemModels>(); 56 for (int i = 0; i < datelist.Count; i++) 57 { 58 List<yatiku_ordersExcel> orderlist = list.Where(p => p.orderdate == DateTime.Parse(datelist[i].ToString())).ToList(); 59 endrowsadd = endrowsadd + orderlist.Count; 60 foreach (yatiku_ordersExcel omodel in orderlist) 61 { 62 List<yatiku_order_itemModels> itemlist = new yatiku_order_itemBLL().GetOrderItemByOrderid(omodel.id); 63 64 if (itemlist.Count > 0) 65 { 66 endrowsadd = endrowsadd + itemlist.Count - 1; 67 int a = 0; 68 int pid = 0; 69 foreach (yatiku_order_itemModels itimmodel in itemlist) 70 { 71 72 73 listresult.Add(itimmodel); 74 75 NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(j); 76 77 rowtemp.CreateCell(0).SetCellValue(Convert.ToDateTime(omodel.orderdate.ToString()).ToString("yyyy-MM-dd")); 78 rowtemp.CreateCell(1).SetCellValue(itimmodel.pname); 79 rowtemp.CreateCell(2).SetCellValue(itimmodel.num.ToString()); 80 rowtemp.CreateCell(3).SetCellValue(itimmodel.price.ToString()); 81 rowtemp.CreateCell(4).SetCellValue(double.Parse(omodel.cost.ToString()).ToString()); 82 if (a == 0) 83 { 84 double cost = double.Parse(omodel.cost.ToString()); 85 rowtemp.CreateCell(5).SetCellValue(((itimmodel.num * itimmodel.price) + cost).ToString()); 86 costsum = costsum + cost;//计算总运费 87 paysum = paysum + (double)(itimmodel.num * itimmodel.price) + cost; 88 } 89 else 90 { 91 rowtemp.CreateCell(5).SetCellValue((itimmodel.num * itimmodel.price).ToString()); 92 paysum = paysum + (double)(itimmodel.num * itimmodel.price); 93 } 94 95 number = number + int.Parse(itimmodel.num.ToString());//总数量 96 pricesum = pricesum + double.Parse((itimmodel.num * itimmodel.price).ToString());//总单价 97 98 rowtemp.CreateCell(6).SetCellValue("/"); 99 rowtemp.CreateCell(7).SetCellType(CellType.Numeric); 100 rowtemp.CreateCell(7).SetCellValue(omodel.tid.ToString()); 101 102 rowtemp.CreateCell(8).SetCellValue(omodel.expname + ":" + omodel.sendcode.ToString()); 103 a++; 104 j++; 105 } 106 //合并单元格 107 sheet1.AddMergedRegion(new CellRangeAddress(j - itemlist.Count, j - 1, 4, 4)); 108 sheet1.AddMergedRegion(new CellRangeAddress(j - itemlist.Count, j - 1, 7, 7)); 109 sheet1.AddMergedRegion(new CellRangeAddress(j - itemlist.Count, j - 1, 8, 8)); 110 111 112 } 113 } 114 sheet1.AddMergedRegion(new CellRangeAddress(starRow, endrowsadd, 0, 0)); 115 starRow = endrowsadd + 1; 116 } 117 //starowsadd = endrowsadd + 1;//第二次合并时从上一次合并到最后一行的行数加1开始 118 119 NPOI.SS.UserModel.IRow row5 = sheet1.CreateRow(5); 120 row5.CreateCell(2).SetCellValue(number.ToString()); 121 row5.CreateCell(3).SetCellValue(pricesum.ToString()); 122 row5.CreateCell(4).SetCellValue(costsum.ToString()); 123 row5.CreateCell(5).SetCellValue(paysum.ToString()); 124 125 NPOI.SS.UserModel.ISheet sheet2 = book.CreateSheet("汇总"); 126 sheet1.SetColumnWidth(0, 20 * 256); 127 sheet1.SetColumnWidth(1, 40 * 256); 128 sheet1.SetColumnWidth(7, 20 * 256); 129 sheet1.SetColumnWidth(8, 20 * 256); 130 131 NPOI.SS.UserModel.IRow rows = sheet2.CreateRow(0); 132 ICell cells = rows.CreateCell(0); 133 cells.CellStyle.Alignment = HorizontalAlignment.Center; 134 cells.SetCellValue(salename + "[" + startime + "至" + endtime + "]" + "数据汇总"); 135 sheet2.AddMergedRegion(new CellRangeAddress(0, 3, 0, 8));//合并单元格,Y坐标:从第0(1)行到第3(4)行,X坐标从第0(1)列到第8(9)列 136 137 NPOI.SS.UserModel.IRow rowst = sheet2.CreateRow(4); 138 ICell cellst = rowst.CreateCell(0); 139 cellst.CellStyle.Alignment = HorizontalAlignment.Center; 140 sheet2.AddMergedRegion(new CellRangeAddress(4, 4, 0, 8)); 141 cellst.SetCellValue("由于一个科目可能同时在两个订单中存在,所以科目的订单数不统计出来"); 142 sheet2.SetColumnWidth(1, 40 * 256);//设置列宽(X坐标从0开始,1为第二列) 143 sheet2.SetColumnWidth(5, 30 * 256); 144 NPOI.SS.UserModel.IRow rows2 = sheet2.CreateRow(5);//创建行 145 rows2.CreateCell(1).SetCellValue("科目"); 146 rows2.CreateCell(2).SetCellValue("数量"); 147 rows2.CreateCell(3).SetCellValue("订单数"); 148 rows2.CreateCell(4).SetCellValue("单价"); 149 rows2.CreateCell(5).SetCellValue("总价(不含运费)"); 150 151 List<int> pidlist = listresult.Select(p => int.Parse(p.pid.ToString())).Distinct().ToList(); 152 int n = 6; 153 int ordercount = 0; 154 int pricecount = 0; 155 int pcount = 0; 156 for (int a = 0; a < pidlist.Count; a++) 157 { 158 NPOI.SS.UserModel.IRow rowtemp = sheet2.CreateRow(n);//创建行 159 160 List<yatiku_order_itemModels> listitemsn = listresult.Where(p => p.pid == pidlist[a]).ToList(); 161 string pname = ""; 162 int pnumber = 0; 163 int pprice = 0; 164 165 List<int> listordercount = listitemsn.Select(p => int.Parse(p.orderid.ToString())).Distinct().ToList(); ; 166 foreach (yatiku_order_itemModels tms in listitemsn) 167 { 168 pname = tms.pname; 169 pnumber = pnumber + int.Parse(tms.num.ToString()); 170 pprice = int.Parse(tms.price.ToString()); 171 // pricecount = pricecount + int.Parse(tms.price.ToString()); 172 // pricecount = pricecount + pprice; 173 } 174 int pcounts = pprice * pnumber; 175 ordercount = ordercount + listordercount.Count; 176 pcount = pcount + pnumber; 177 rowtemp.CreateCell(1).SetCellValue(pname); 178 rowtemp.CreateCell(2).SetCellValue(pnumber.ToString()); 179 rowtemp.CreateCell(3).SetCellValue("/"); 180 rowtemp.CreateCell(4).SetCellValue(pprice); 181 rowtemp.CreateCell(5).SetCellValue(pprice * pnumber); 182 pricecount = pricecount + pcounts; 183 n++; 184 } 185 186 List<int> orderidlist = listresult.Select(p => int.Parse(p.orderid.ToString())).Distinct().ToList(); 187 188 NPOI.SS.UserModel.IRow rowtemplast = sheet2.CreateRow(n + 2); 189 rowtemplast.CreateCell(2).SetCellValue("总数量"); 190 rowtemplast.CreateCell(3).SetCellValue("总订单数"); 191 rowtemplast.CreateCell(5).SetCellValue("总计(人民币)"); 192 193 NPOI.SS.UserModel.IRow rowtemplasts = sheet2.CreateRow(n + 3); 194 rowtemplasts.CreateCell(2).SetCellValue(pcount.ToString()); 195 rowtemplasts.CreateCell(3).SetCellValue(orderidlist.Count.ToString()); 196 rowtemplasts.CreateCell(5).SetCellValue(pricecount.ToString()); 197 198 199 // 写入到客户端 200 System.IO.MemoryStream ms = new System.IO.MemoryStream(); 201 book.Write(ms); 202 ms.Seek(0, SeekOrigin.Begin); 203 204 string filename = salename + "报表.xls"; 205 206 return File(ms, "application/vnd.ms-excel", filename); 207 208 //return null; 209 }