@ -1,20 +1,28 @@
package com.storeroom.modules.common ;
import com.storeroom.modules.archives.domain.BorrowArchives ;
import com.storeroom.modules.archives.domain.vo.BorrowBillDetailsVO ;
import org.apache.poi.hssf.usermodel.* ;
import org.apache.poi.ss.usermodel.BorderStyle ;
import org.apache.poi.ss.usermodel.CellStyle ;
import org.apache.poi.ss.usermodel.HorizontalAlignment ;
import org.apache.poi.ss.usermodel.VerticalAlignment ;
import org.apache.poi.ss.util.CellRangeAddress ;
import org.apache.poi.xssf.usermodel.* ;
import javax.servlet.http.HttpServletResponse ;
import java.io.UnsupportedEncodingException ;
import java.net.URLEncoder ;
import java.sql.Timestamp ;
import java.text.SimpleDateFormat ;
import java.util.Date ;
import java.util.List ;
public class ExcelUtil {
private static SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy-MM-dd" ) ;
private static SimpleDateFormat sdf1 = new SimpleDateFormat ( "yyyy-MM-dd hh:mm:ss" ) ;
/ * *
* 导出Excel
* @param sheetName sheet名称
@ -97,15 +105,106 @@ public class ExcelUtil {
textStyle . setBorderBottom ( BorderStyle . THIN ) ;
textStyle . setBorderRight ( BorderStyle . THIN ) ;
textStyle . setBorderLeft ( BorderStyle . THIN ) ;
textStyle . setBorderTop ( BorderStyle . THIN ) ;
textStyle . setFont ( dataSetFont ) ;
/ / 数据内容对齐方式 : 居中
textStyle . setAlignment ( HorizontalAlignment . CENTER ) ;
textStyle . setVerticalAlignment ( VerticalAlignment . CENTER ) ;
/ / 行
int index = 0 ;
String [ ] billTitle = { "单据号" , "借阅人" , "所属部门" , "证件类型" , "证件号码" , "电话号码" , "借阅目的" , "借阅日期" , "操作时间" , "借阅状态" } ;
String [ ] archivesTitle = { "序号" , "门类名称" , "题名" , "档号" , "盒名称" , "" , "存放位置" , "" , "借阅状态" } ;
for ( BorrowBillDetailsVO vo : bills ) {
/ / 清单title
XSSFRow billTitleRow = sheet . createRow ( index ) ;
List < BorrowArchives > borrowArchives = vo . getBorrowArchives ( ) ;
for ( int i = 0 ; i < billTitle . length ; i + + ) {
XSSFCell cell = billTitleRow . createCell ( i ) ;
cell . setCellStyle ( textStyle ) ;
cell . setCellValue ( billTitle [ i ] ) ;
}
index + + ;
/ / 清单值
XSSFRow billValueRow = sheet . createRow ( index ) ;
XSSFCell billValueCell0 = billValueRow . createCell ( 0 ) ; billValueCell0 . setCellValue ( vo . getId ( ) ) ; billValueCell0 . setCellStyle ( textStyle ) ;
XSSFCell billValueCell1 = billValueRow . createCell ( 1 ) ; billValueCell1 . setCellValue ( vo . getBorrower ( ) . getBorrowerName ( ) ) ; billValueCell1 . setCellStyle ( textStyle ) ;
XSSFCell billValueCell2 = billValueRow . createCell ( 2 ) ; billValueCell2 . setCellValue ( vo . getBorrower ( ) . getDepartment ( ) ) ; billValueCell2 . setCellStyle ( textStyle ) ;
XSSFCell billValueCell3 = billValueRow . createCell ( 3 ) ; billValueCell3 . setCellValue ( vo . getBorrower ( ) . getCardType ( ) ) ; billValueCell3 . setCellStyle ( textStyle ) ;
XSSFCell billValueCell4 = billValueRow . createCell ( 4 ) ; billValueCell4 . setCellValue ( vo . getBorrower ( ) . getIdcard ( ) ) ; billValueCell4 . setCellStyle ( textStyle ) ;
XSSFCell billValueCell5 = billValueRow . createCell ( 5 ) ; billValueCell5 . setCellValue ( vo . getBorrower ( ) . getPhone ( ) ) ; billValueCell5 . setCellStyle ( textStyle ) ;
XSSFCell billValueCell6 = billValueRow . createCell ( 6 ) ; billValueCell6 . setCellValue ( vo . getPurpose ( ) ) ; billValueCell6 . setCellStyle ( textStyle ) ;
XSSFCell billValueCell7 = billValueRow . createCell ( 7 ) ; billValueCell7 . setCellValue ( sdf . format ( new Date ( vo . getBorrowStart ( ) . getTime ( ) ) ) + "至" + sdf . format ( new Date ( vo . getBorrowEnd ( ) . getTime ( ) ) ) ) ; billValueCell7 . setCellStyle ( textStyle ) ;
XSSFCell billValueCell8 = billValueRow . createCell ( 8 ) ; billValueCell8 . setCellValue ( sdf1 . format ( new Date ( vo . getCreateTime ( ) . getTime ( ) ) ) ) ; billValueCell8 . setCellStyle ( textStyle ) ;
Integer borrowType = vo . getBorrowType ( ) ;
String showBorrowType = "" ;
if ( borrowType = = 2 ) {
showBorrowType = "待借阅" ;
} else if ( borrowType = = 3 & & vo . getBorrowEnd ( ) . getTime ( ) > = new Timestamp ( System . currentTimeMillis ( ) ) . getTime ( ) ) {
showBorrowType = "待归还" ;
} else if ( borrowType = = 3 & & vo . getBorrowEnd ( ) . getTime ( ) < new Timestamp ( System . currentTimeMillis ( ) ) . getTime ( ) ) {
showBorrowType = "逾期" ;
} else if ( borrowType = = 4 ) {
showBorrowType = "已归还" ;
}
XSSFCell billValueCell9 = billValueRow . createCell ( 9 ) ; billValueCell9 . setCellValue ( showBorrowType ) ; billValueCell9 . setCellStyle ( textStyle ) ;
Integer billRowStart = index ;
if ( null ! = borrowArchives & & borrowArchives . size ( ) ! = 0 ) {
index + + ;
XSSFRow archivesTitleRow = sheet . createRow ( index ) ;
XSSFCell cell0 = archivesTitleRow . createCell ( 0 ) ; cell0 . setCellStyle ( textStyle ) ;
for ( int i = 0 ; i < archivesTitle . length ; i + + ) {
XSSFCell cell = archivesTitleRow . createCell ( i + 1 ) ;
cell . setCellStyle ( textStyle ) ;
cell . setCellValue ( archivesTitle [ i ] ) ;
}
sheet . addMergedRegion ( new CellRangeAddress ( index , index , 5 , 6 ) ) ;
sheet . addMergedRegion ( new CellRangeAddress ( index , index , 7 , 8 ) ) ;
for ( int ba = 0 ; ba < borrowArchives . size ( ) ; ba + + ) {
index + + ;
BorrowArchives borrowArchive = borrowArchives . get ( ba ) ;
XSSFRow archivesValueRow = sheet . createRow ( index ) ;
XSSFCell baValueCell0 = archivesValueRow . createCell ( 0 ) ; baValueCell0 . setCellStyle ( textStyle ) ;
XSSFCell baValueCell1 = archivesValueRow . createCell ( 1 ) ; baValueCell1 . setCellValue ( ba + 1 ) ; baValueCell1 . setCellStyle ( textStyle ) ;
XSSFCell baValueCell2 = archivesValueRow . createCell ( 2 ) ; baValueCell2 . setCellValue ( borrowArchive . getCategoryName ( ) ) ; baValueCell2 . setCellStyle ( textStyle ) ;
XSSFCell baValueCell3 = archivesValueRow . createCell ( 3 ) ; baValueCell3 . setCellValue ( borrowArchive . getMaintitle ( ) ) ; baValueCell3 . setCellStyle ( textStyle ) ;
XSSFCell baValueCell4 = archivesValueRow . createCell ( 4 ) ; baValueCell4 . setCellValue ( borrowArchive . getArchiveNo ( ) ) ; baValueCell4 . setCellStyle ( textStyle ) ;
XSSFCell baValueCell5 = archivesValueRow . createCell ( 5 ) ; baValueCell5 . setCellValue ( borrowArchive . getCaseName ( ) ) ; baValueCell5 . setCellStyle ( textStyle ) ;
XSSFCell baValueCell6 = archivesValueRow . createCell ( 6 ) ; baValueCell6 . setCellStyle ( textStyle ) ;
XSSFCell baValueCell7 = archivesValueRow . createCell ( 7 ) ; baValueCell7 . setCellValue ( borrowArchive . getFolderLocationDetails ( ) ) ; baValueCell7 . setCellStyle ( textStyle ) ;
XSSFCell baValueCell8 = archivesValueRow . createCell ( 8 ) ; baValueCell8 . setCellStyle ( textStyle ) ;
Integer borrowArchiType = borrowArchive . getBorrowType ( ) ;
String showBorrowArchiType = "" ;
if ( borrowArchiType = = 2 ) {
showBorrowArchiType = "待借阅" ;
} else if ( borrowArchiType = = 3 & & borrowArchive . getEndTime ( ) . getTime ( ) > = new Timestamp ( System . currentTimeMillis ( ) ) . getTime ( ) ) {
showBorrowArchiType = "待归还" ;
} else if ( borrowArchiType = = 3 & & borrowArchive . getEndTime ( ) . getTime ( ) < new Timestamp ( System . currentTimeMillis ( ) ) . getTime ( ) ) {
showBorrowArchiType = "逾期" ;
} else if ( borrowArchiType = = 4 ) {
showBorrowArchiType = "已归还" ;
}
XSSFCell baValueCell9 = archivesValueRow . createCell ( 9 ) ; baValueCell9 . setCellValue ( showBorrowArchiType ) ; baValueCell9 . setCellStyle ( textStyle ) ;
sheet . addMergedRegion ( new CellRangeAddress ( index , index , 5 , 6 ) ) ;
sheet . addMergedRegion ( new CellRangeAddress ( index , index , 7 , 8 ) ) ;
}
Integer billRowEnd = index ;
sheet . addMergedRegion ( new CellRangeAddress ( billRowStart , billRowEnd , 0 , 0 ) ) ;
}
index + + ;
XSSFRow nullRow = sheet . createRow ( index ) ;
index + + ;
}
/ / 自适应单元格
for ( int i = 0 ; i < sheet . getLastRowNum ( ) ; i + + ) {
sheet . autoSizeColumn ( i ) ;
sheet . setColumnWidth ( i , sheet . getColumnWidth ( i ) * 17 / 10 ) ;
}
return workbook ;
}