記錄一下,NPOI導出電子表格代碼(包含合並單元格式)


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         }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM