1. 程式人生 > >java生成excel

java生成excel


//判斷程式執行的作業系統
String rootPath="";
if("\\".equals(File.separator)){//windos
File directory = new File("");//設定為當前資料夾
rootPath = directory.getCanonicalPath();
rootPath = rootPath.replace("/", "\\");
rootPath = rootPath+"\\"+fileName+".xlsx";


}else if("/".equals(File.separator)){//linux
//伺服器中的excel生成的路徑
rootPath = filePath+"/"+fileName+".xlsx";
}
System.out.println(rootPath);
FileOutputStream file=new FileOutputStream(rootPath);//建立檔案輸出流
BufferedOutputStream out=new BufferedOutputStream(file);
XSSFWorkbook workbook = new XSSFWorkbook();

for(ExcelBean bean : listBean){

//顯示的匯出表的標題
String title=bean.getTitle();

//匯出表的列名
String[] rowName=bean.getRowName() ;
String rowTitleName=bean.getTitleName();


List<String[]> dataList = bean.getDataList();
XSSFSheet sheet = workbook.createSheet(title); // 建立工作表

//sheet樣式定義【getColumnTopStyle()/getStyle()均為自定義方法 - 在下面 - 可擴充套件】
XSSFCellStyle columnTopStyle = excelType.getColumnTopStyle(workbook);//獲取列頭樣式物件
XSSFCellStyle style = excelType.getStyle(workbook); //單元格樣式物件

// 定義所需列數
int columnNum = rowName.length;
XSSFRow rowRowTitle;
XSSFRow rowRowName;
boolean titleName=rowTitleName!=null;

if(titleName){
rowRowTitle= sheet.createRow(0); // 在索引2的位置建立行(最頂端的行開始的第二行)
rowRowName= sheet.createRow(1); // 在索引2的位置建立行(最頂端的行開始的第二行)
XSSFCell cell = rowRowTitle.createCell(0);


cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(columnTopStyle);
CellRangeAddress region = new CellRangeAddress(0, 0, 0, columnNum-1);

sheet.addMergedRegion(region);

cell.setCellValue(rowTitleName);

}else{
rowRowName = sheet.createRow(0); // 在索引2的位置建立行(最頂端的行開始的第二行)
}


// 將列頭設定到sheet的單元格中
for(int n=0;n<columnNum;n++){

XSSFCell cellRowName = rowRowName.createCell(n); //建立列頭對應個數的單元格
cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING); //設定列頭單元格的資料型別
XSSFRichTextString text = new XSSFRichTextString(rowName[n]);
cellRowName.setCellValue(text); //設定列頭單元格的值
cellRowName.setCellStyle(columnTopStyle);

}

//將查詢出的資料設定到sheet對應的單元格中
for(int i=0;i<dataList.size();i++){

Object[] obj = dataList.get(i);//遍歷每個物件
XSSFRow row;
if(titleName){
row = sheet.createRow(i+2);//建立所需的行數(從第二行開始寫資料)
}else{
row = sheet.createRow(i+1);//建立所需的行數(從第二行開始寫資料)
}


for(int j=0; j<obj.length; j++){
XSSFCell cell = null; //設定單元格的資料型別
if(j == 0){
cell = row.createCell(j,HSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue(i+1);
// cell.setCellStyle(style); //設定單元格樣式
}else{
cell = row.createCell(j,HSSFCell.CELL_TYPE_STRING);
if(!"".equals(obj[j]) && obj[j] != null){
cell.setCellValue(obj[j].toString()); //設定單元格的值
// cell.setCellStyle(style); //設定單元格樣式
}
if(j==obj.length-1){

}else{
// cell.setCellStyle(style);
}
}

}
}
//讓列寬隨著匯出的列長自動適應
for (int colNum = 0; colNum < columnNum; colNum++) {
int columnWidth = sheet.getColumnWidth(colNum) / 256;
for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {
XSSFRow currentRow;
//當前行未被使用過
if (sheet.getRow(rowNum) == null) {
currentRow = sheet.createRow(rowNum);
} else {
currentRow = sheet.getRow(rowNum);
}
if (currentRow.getCell(colNum) != null) {
XSSFCell currentCell = currentRow.getCell(colNum);
if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
int length = currentCell.getStringCellValue().getBytes().length;
if (columnWidth < length) {
columnWidth = length;
}
}
}
if (currentRow.getCell(colNum) != null) {
XSSFCell currentCell = currentRow.getCell(colNum);
if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
int length = 0;
try {
length = currentCell.getStringCellValue().getBytes().length;
} catch (Exception e) {
e.printStackTrace();
}
if (columnWidth < length) {
columnWidth = length;
}
}
}

}

if(colNum == 0){
sheet.setColumnWidth(colNum, (columnWidth-2) * 256);
}else{
sheet.setColumnWidth(colNum, (columnWidth+4) * 256);
}
}

}

if(workbook !=null){
try{
workbook.write(out);
}catch (IOException e) {
e.printStackTrace();
}
}
return rootPath;


}

public CreatExcelUtil(){

}

public CreatExcelUtil(String filepath) {
if(filepath==null){
return;
}
String ext = filepath.substring(filepath.lastIndexOf("."));
try {
InputStream is = new FileInputStream(filepath);
if(".xls".equals(ext)){
wb = new HSSFWorkbook(is);
}else if(".xlsx".equals(ext)){
wb = new XSSFWorkbook(is);
}else{
wb=null;
}
} catch (FileNotFoundException e) {
logger.error("FileNotFoundException", e);
} catch (IOException e) {
logger.error("IOException", e);
}
}