歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java 操作Excel 分析 導出Execl

Java 操作Excel 分析 導出Execl

日期:2017/3/1 10:07:14   编辑:Linux編程

public String getExport(QcyOweOrder obj, String type, String path) throws Exception {
// TODO 導出
String url = null;
// 導出某個模版
if (obj != null && obj.getId() != null && type == null) {
obj = this.findById(obj);

url = path + "\\" +"有限公司_欠貨明細單"+obj.getNo()+".xls";


//首先要使用Workbook類的工廠方法創建一個可寫入的工作薄(Workbook)對象

WritableWorkbook book = Workbook.createWorkbook(new File(url));


//創建一個可寫入的工作表
//Workbook的createSheet方法有兩個參數,第一個是工作表的名稱,第二個是工作表在工作薄中的位置

WritableSheet sheet = book.createSheet("欠貨明細單", 0);


// 畫出表格樣式,第一個參數是列的意思,第二個參數是設置列的寬度
sheet.setColumnView(0, 15);
sheet.setColumnView(1, 15);
sheet.setColumnView(2, 15);
sheet.setColumnView(3, 15);
sheet.setColumnView(4, 20);
sheet.setColumnView(5, 15);
sheet.setColumnView(6, 15);
sheet.setColumnView(7, 15);
sheet.setColumnView(8, 15);
sheet.setColumnView(9, 15);
sheet.setColumnView(10, 15);
sheet.setColumnView(11, 15);
sheet.setColumnView(12, 15);

// 標題格式設置WritableFont字符串的格式化涉及到的是字體、粗細、字號等元素

WritableFont fontTableHeader = new WritableFont(WritableFont.ARIAL, 20, WritableFont.BOLD);


// 處代碼使用了WritableCellFormat類,這個類非常重要,通過它可以指定單元格的各種 屬性,後面的單元格格式化中會有更多描述。
WritableCellFormat formatTableHeader = new WritableCellFormat();
formatTableHeader.setFont(fontTableHeader);
formatTableHeader.setAlignment(jxl.format.Alignment.CENTRE);
formatTableHeader.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);

//處使用了Label類的構造子,指定了字串被賦予那種格式第一列第一行
Label labelTable = new Label(0, 0, "有限公司", formatTableHeader);//0列0行設置文字
Label labelTable1 = new Label(0, 1, "欠 貨 明 細 單", formatTableHeader);//0列1行設置的內容
sheet.mergeCells(0, 0, 13, 0);//合並功能(開始列,開始行,結束列,結束行)
sheet.mergeCells(0, 1, 13, 1);
sheet.addCell(labelTable);
sheet.addCell(labelTable1);

// 表頭格式設置
WritableFont fontColumnHeader = new WritableFont(WritableFont.ARIAL, 14, WritableFont.BOLD);

WritableCellFormat formatColumnHeader = new WritableCellFormat();
formatColumnHeader.setFont(fontColumnHeader);
formatColumnHeader.setAlignment(jxl.format.Alignment.CENTRE);
formatColumnHeader.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);

//設置列的標題
Label labelColumn0 = new Label(0, 2, "客戶", formatColumnHeader);
Label labelColumn1 = new Label(1, 2, "開單日期", formatColumnHeader);
Label labelColumn2 = new Label(2, 2, "送貨地址", formatColumnHeader);
Label labelColumn3 = new Label(3, 2, "訂單編號", formatColumnHeader);
Label labelColumn4 = new Label(4, 2, "存貨名稱", formatColumnHeader);
Label labelColumn5 = new Label(5, 2, "主要單位", formatColumnHeader);
Label labelColumn6 = new Label(6, 2, "訂貨數量", formatColumnHeader);
Label labelColumn7 = new Label(7, 2, "審核數量", formatColumnHeader);
Label labelColumn8 = new Label(8, 2, "欠貨數量", formatColumnHeader);
Label labelColumn9 = new Label(9, 2, "單價", formatColumnHeader);
Label labelColumn10 = new Label(10, 2, "金額", formatColumnHeader);
Label labelColumn11 = new Label(11, 2, "本次出貨量", formatColumnHeader);
Label labelColumn12 = new Label(12, 2, "累計出貨量", formatColumnHeader);

sheet.addCell(labelColumn0);
sheet.addCell(labelColumn1);
sheet.addCell(labelColumn2);
sheet.addCell(labelColumn3);
sheet.addCell(labelColumn4);
sheet.addCell(labelColumn5);
sheet.addCell(labelColumn6);
sheet.addCell(labelColumn7);
sheet.addCell(labelColumn8);
sheet.addCell(labelColumn9);
sheet.addCell(labelColumn10);
sheet.addCell(labelColumn11);
sheet.addCell(labelColumn12);
// 內容格式設置
WritableFont fontContentHeader = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD);
WritableCellFormat formatContentHeader = new WritableCellFormat();
formatContentHeader.setFont(fontContentHeader);
formatContentHeader.setAlignment(jxl.format.Alignment.CENTRE);
formatContentHeader.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);

// 遍歷輸出list
int totalAboveRow = 3;// 上部高度
float totalsum = 0;
List<Object[]> list = (List<Object[]>) this.findAllRBpdf(obj);

Label itemName = new Label(0, totalAboveRow, obj.getRequestBranchName(), formatContentHeader);//客戶信息
sheet.addCell(itemName);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Label date = new Label(1, totalAboveRow, sdf.format(obj.getSalesDate()), formatContentHeader);//開單時間
sheet.addCell(date);

Label address = new Label(2, totalAboveRow, "", formatContentHeader);// 送貨地址
sheet.addCell(address);

Label bomLable9 = null;
Label bomLable11 = null;
BdSecondaryUnit secoUnit =null;

for (int i = 0; i < list.size(); i++) {
Double price =0.0;
String priceStr ="";
String orderNumber ="";
String auditNumber ="";
String oweNumber ="";
String saleNumber ="";
//累計出貨

String totalGoOutStr ="";
if(list.get(i)[9]!=null){
secoUnit = (BdSecondaryUnit) this.getQcyOweOrderDAO().getObject("from BdSecondaryUnit where product.id=" +list.get(i)[9]);
}
if (null != list.get(i)[4] && null != list.get(i)[10]&& null != list.get(i)[6]) {
price = Double.parseDouble(list.get(i)[4].toString())/Double.parseDouble(list.get(i)[10].toString())*Double.parseDouble( list.get(i)[6].toString()) ;
priceStr = Tool.keep4DecimalRounding1ToString(price);
}
totalsum += price;
//訂貨量
if (null != list.get(i)[3]&&null != list.get(i)[10]&& Double.parseDouble(list.get(i)[3].toString())>=Double.parseDouble(list.get(i)[10].toString())) {
orderNumber= (int) (Double.parseDouble(list.get(i)[3].toString())/Double.parseDouble( list.get(i)[10].toString()))+ list.get(i)[2].toString();
}
if (null != list.get(i)[3]&&null != list.get(i)[10]&& null != secoUnit&&secoUnit.getSecondary2Basic()!=null &&
Double.parseDouble(list.get(i)[3].toString()) % Double.parseDouble(list.get(i)[10].toString()) != 0) {
orderNumber=(orderNumber + Tool.keep2DecimalRounding((Double.parseDouble(list.get(i)[3].toString()) - (int) (Double.parseDouble(list.get(i)[3].toString())/ Double.parseDouble(list.get(i)[10].toString()))
* Double.parseDouble(list.get(i)[10].toString()))
/ secoUnit.getSecondary2Basic()) + secoUnit.getUnit().getName());
}else {
if (null != list.get(i)[3]&& null != list.get(i)[10]&& Double.parseDouble(list.get(i)[3].toString()) %Double.parseDouble(list.get(i)[10].toString())> 0&&null!= list.get(i)[11])
orderNumber=(orderNumber + Tool.keep2DecimalRounding((Double.parseDouble(list.get(i)[3].toString()) - (int) (Double.parseDouble(list.get(i)[3].toString()) / Double.parseDouble(list.get(i)[10].toString())) * Double.parseDouble(list.get(i)[10].toString())))
+ list.get(i)[11].toString());
}
//本次出貨量
if (null != list.get(i)[4]&&null != list.get(i)[10]&& Double.parseDouble(list.get(i)[4].toString())>=Double.parseDouble(list.get(i)[10].toString())) {
saleNumber= (int) (Double.parseDouble(list.get(i)[4].toString())/Double.parseDouble( list.get(i)[10].toString()))+ list.get(i)[2].toString();
}
if (null != list.get(i)[4]&&null != list.get(i)[10]&& null != secoUnit&&secoUnit.getSecondary2Basic()!=null &&
Double.parseDouble(list.get(i)[4].toString()) % Double.parseDouble(list.get(i)[10].toString()) != 0) {
saleNumber=(saleNumber + Tool.keep2DecimalRounding((Double.parseDouble(list.get(i)[4].toString()) - (int) (Double.parseDouble(list.get(i)[4].toString())/ Double.parseDouble(list.get(i)[10].toString()))
* Double.parseDouble(list.get(i)[10].toString()))
/ secoUnit.getSecondary2Basic()) + secoUnit.getUnit().getName());
}else {
if (null != list.get(i)[4]&& null != list.get(i)[10]&& Double.parseDouble(list.get(i)[4].toString()) %Double.parseDouble(list.get(i)[10].toString())> 0&&null!= list.get(i)[11])
saleNumber=(saleNumber + Tool.keep2DecimalRounding((Double.parseDouble(list.get(i)[4].toString()) - (int) (Double.parseDouble(list.get(i)[4].toString()) / Double.parseDouble(list.get(i)[10].toString())) * Double.parseDouble(list.get(i)[10].toString())))
+ list.get(i)[11].toString());
}
//審核量
if (null != list.get(i)[8]&&null != list.get(i)[10]&& Double.parseDouble(list.get(i)[8].toString())>=Double.parseDouble(list.get(i)[10].toString())) {
auditNumber= (int) (Double.parseDouble(list.get(i)[8].toString())/Double.parseDouble( list.get(i)[10].toString()))+ list.get(i)[2].toString();
}
if (null != list.get(i)[8]&&null != list.get(i)[10]&& null != secoUnit&&secoUnit.getSecondary2Basic()!=null &&
Double.parseDouble(list.get(i)[8].toString()) % Double.parseDouble(list.get(i)[10].toString()) != 0) {
auditNumber=(auditNumber + Tool.keep2DecimalRounding((Double.parseDouble(list.get(i)[8].toString()) - (int) (Double.parseDouble(list.get(i)[8].toString())/ Double.parseDouble(list.get(i)[10].toString()))
* Double.parseDouble(list.get(i)[10].toString()))
/ secoUnit.getSecondary2Basic()) + secoUnit.getUnit().getName());
}else {
if (null != list.get(i)[8]&& null != list.get(i)[10]&& Double.parseDouble(list.get(i)[8].toString()) %Double.parseDouble(list.get(i)[10].toString())> 0&&null!= list.get(i)[11])
auditNumber=(auditNumber +Tool.keep2DecimalRounding( (Double.parseDouble(list.get(i)[8].toString()) - (int) (Double.parseDouble(list.get(i)[8].toString()) / Double.parseDouble(list.get(i)[10].toString())) * Double.parseDouble(list.get(i)[10].toString())))
+ list.get(i)[11].toString());
}
//欠貨量
if (null != list.get(i)[5]&&null != list.get(i)[10]&& Double.parseDouble(list.get(i)[5].toString())>=Double.parseDouble(list.get(i)[10].toString())) {
oweNumber= (int) (Double.parseDouble(list.get(i)[5].toString())/Double.parseDouble( list.get(i)[10].toString()))+ list.get(i)[2].toString();
}
if (null != list.get(i)[5]&&null != list.get(i)[10]&& null != secoUnit&&secoUnit.getSecondary2Basic()!=null &&
Double.parseDouble(list.get(i)[5].toString()) % Double.parseDouble(list.get(i)[10].toString()) != 0) {
oweNumber=(oweNumber + Tool.keep2DecimalRounding((Double.parseDouble(list.get(i)[5].toString()) - (int) (Double.parseDouble(list.get(i)[5].toString())/ Double.parseDouble(list.get(i)[10].toString()))
* Double.parseDouble(list.get(i)[10].toString()))
/ secoUnit.getSecondary2Basic()) + secoUnit.getUnit().getName());
}else {
if (null != list.get(i)[5]&& null != list.get(i)[10]&& Double.parseDouble(list.get(i)[5].toString()) %Double.parseDouble(list.get(i)[10].toString())> 0&&null!= list.get(i)[11])
oweNumber=(oweNumber + Tool.keep2DecimalRounding((Double.parseDouble(list.get(i)[5].toString()) - (int) (Double.parseDouble(list.get(i)[5].toString()) / Double.parseDouble(list.get(i)[10].toString())) * Double.parseDouble(list.get(i)[10].toString())))
+ list.get(i)[11].toString());
}

Double totalGoOut = 0.0d;
if(null != list.get(i)[4] && null != list.get(i)[5] && null != list.get(i)[8]){
totalGoOut =Double.parseDouble(list.get(i)[8].toString()) - Double.parseDouble(list.get(i)[5].toString())+Double.parseDouble(list.get(i)[4].toString());

//累計出貨量
if (null != list.get(i)[10]&& totalGoOut>=Double.parseDouble(list.get(i)[10].toString())) {
totalGoOutStr= (int) (totalGoOut/Double.parseDouble( list.get(i)[10].toString()))+ list.get(i)[2].toString();
}
if (null != list.get(i)[10]&& null != secoUnit&&secoUnit.getSecondary2Basic()!=null && totalGoOut% Double.parseDouble(list.get(i)[10].toString()) != 0) {
totalGoOutStr=(totalGoOutStr + Tool.keep2DecimalRounding((totalGoOut- (int) (Double.parseDouble(list.get(i)[5].toString())/ Double.parseDouble(list.get(i)[10].toString()))
* Double.parseDouble(list.get(i)[10].toString()))
/ secoUnit.getSecondary2Basic()) + secoUnit.getUnit().getName());
}else {
if (null != list.get(i)[10]&&totalGoOut%Double.parseDouble(list.get(i)[10].toString())> 0&&null!= list.get(i)[11])
totalGoOutStr=(totalGoOutStr + Tool.keep2DecimalRounding((totalGoOut - (int) (totalGoOut/ Double.parseDouble(list.get(i)[10].toString())) * Double.parseDouble(list.get(i)[10].toString())))
+ list.get(i)[11].toString());
}
}

Label bomLable0 = new Label(3, totalAboveRow + i, "" + isNull(list.get(i)[0]), formatContentHeader);
Label bomLable1 = new Label(4, totalAboveRow + i, "" + isNull(list.get(i)[1]), formatContentHeader);
Label bomLable2 = new Label(5, totalAboveRow + i, "" + isNull(list.get(i)[2]), formatContentHeader);
Label bomLable3 = new Label(6, totalAboveRow + i, "" + isNull(orderNumber), formatContentHeader);//訂貨量
Label bomLable4 = new Label(7, totalAboveRow + i, "" + isNull(auditNumber), formatContentHeader);//審核量
Label bomLable5 = new Label(8, totalAboveRow + i, "" + isNull(oweNumber), formatContentHeader);//欠貨量
Label bomLable6 = new Label(9, totalAboveRow + i, "" + isNull(list.get(i)[6]), formatContentHeader);
Label bomLable100 = new Label(10, totalAboveRow + i, "" + isNull(priceStr), formatContentHeader);
Label bomLable111 = new Label(11, totalAboveRow + i, "" + isNull(saleNumber), formatContentHeader);
Label bomLable120 = new Label(12, totalAboveRow + i, "" + isNull(totalGoOutStr), formatContentHeader);

bomLable9 = new Label(0, totalAboveRow + 1 + i, "合 計", formatColumnHeader);
bomLable11 = new Label(10, totalAboveRow + 1 + i, "" +Tool.keep4DecimalRounding1ToString( totalsum), formatColumnHeader);

sheet.addCell(bomLable0);
sheet.addCell(bomLable1);
sheet.addCell(bomLable2);
sheet.addCell(bomLable3);
sheet.addCell(bomLable4);
sheet.addCell(bomLable5);
sheet.addCell(bomLable6);
sheet.addCell(bomLable100);
sheet.addCell(bomLable111);
sheet.addCell(bomLable120);
}
sheet.addCell(bomLable9);
sheet.addCell(bomLable11);
if (book != null) {
book.write();
book.close();
}
}
return url;

}

具體格式如下:

Copyright © Linux教程網 All Rights Reserved