javaエクスポートexcelファイル(圧縮後にエクスポート)
11676 ワード
excel , , : poi 2003 excel
:
public File writeExcel(List
public String getFile(T_L000800 tL000800){
// ,
List
: , excel ,
package com.tansun.doservice.httpService.excel;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.jdbc.core.JdbcTemplate;
import webbean.R_S000130_Rows;
import webbean.T_L000800;
public class SearchPhone{
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
public String getFile(T_L000800 tL000800){
String title="111";
String heads[]={" "," "," "," "," "," "," "," "," 2"," "," 1"," 2"};
String returnString = "";
try {
returnString = this.print(title, heads, result,tL000800.getTuserId());
} catch (Exception e) {
e.printStackTrace();
}
return returnString;
}
private String print(String title,String heads[],List list,String userId) throws Exception{
// , 2003
Workbook wb = new HSSFWorkbook();
// sheet
Sheet sheet = wb.createSheet();
//
Row row = null;
//
Cell cell = null;
// ,
int rowNo = 0;
//
int cellNo = 1;
//=========== begin===================
//
row = sheet.createRow(rowNo++);
//
row.setHeightInPoints(36f);
cell = row.createCell(cellNo);
// 2012 8 2016-12 2016-07
cell.setCellValue(title);
//
//
sheet.addMergedRegion(new CellRangeAddress(0,0,1,10));
//
CellStyle bigTitle = this.bigTitle(wb);
cell.setCellStyle(bigTitle);
//=========== end===================
// =========== start===================
//
sheet.setColumnWidth(0, 256*5);
sheet.setColumnWidth(1, 256*10);
sheet.setColumnWidth(2, 256*16);
sheet.setColumnWidth(3, 256*20);
sheet.setColumnWidth(4, 256*20);
sheet.setColumnWidth(5, 256*14);
sheet.setColumnWidth(6, 256*10);
sheet.setColumnWidth(7, 256*10);
sheet.setColumnWidth(8, 256*16);
sheet.setColumnWidth(9, 256*16);
sheet.setColumnWidth(10, 256*14);
sheet.setColumnWidth(11, 256*12);
sheet.setColumnWidth(12, 256*14);
//
//
row = sheet.createRow(rowNo++);
CellStyle headStyle = wb.createCellStyle();
// ,
for (String head : heads) {
//
// , cellNo +1
cell = row.createCell(cellNo++);
//
Font font = wb.createFont();
font.setFontName(" ");
font.setFontHeightInPoints((short)12);
headStyle.setFont(font);
headStyle.setAlignment(CellStyle.ALIGN_CENTER); //
headStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); //
headStyle.setBorderTop(CellStyle.BORDER_THIN); //
headStyle.setBorderBottom(CellStyle.BORDER_THIN); //
headStyle.setBorderLeft(CellStyle.BORDER_THIN); //
headStyle.setBorderRight(CellStyle.BORDER_THIN); //
cell.setCellStyle(headStyle);
//
cell.setCellValue(head);
}
//==================== end=========================
//==================== start=========================
//
int index=1;
CellStyle textStyle = wb.createCellStyle();
Font font = wb.createFont();
font.setFontName("Times New Roman");
font.setFontHeightInPoints((short)10);
textStyle.setFont(font);
textStyle.setAlignment(CellStyle.ALIGN_LEFT); //
textStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); //
textStyle.setBorderTop(CellStyle.BORDER_THIN); //
textStyle.setBorderBottom(CellStyle.BORDER_THIN); //
textStyle.setBorderLeft(CellStyle.BORDER_THIN); //
textStyle.setBorderRight(CellStyle.BORDER_THIN); //
for (R_S000130_Rows rows : list) {
// 1
cellNo = 1;
//
//
row = sheet.createRow(rowNo++);
//
row.setHeightInPoints(24f);
//
cell = row.createCell(cellNo++);
//
cell.setCellStyle(textStyle);
//
cell.setCellValue(index++);
//
cell = row.createCell(cellNo++);
//
cell.setCellStyle(textStyle);
//
cell.setCellValue(rows.getCustomerName());
//
cell = row.createCell(cellNo++);
//
cell.setCellStyle(textStyle);
//
cell.setCellValue(rows.getCartNum());
//
cell = row.createCell(cellNo++);
//
cell.setCellStyle(textStyle);
//
cell.setCellValue(rows.getLoanProduct());
//
cell = row.createCell(cellNo++);
//
cell.setCellStyle(textStyle);
//
cell.setCellValue(rows.getLoanSum());
//
cell = row.createCell(cellNo++);
//
cell.setCellStyle(textStyle);
//
cell.setCellValue(rows.getPaymentDate());
//
cell = row.createCell(cellNo++);
//
cell.setCellStyle(textStyle);
//
cell.setCellValue(rows.getPaymentSum());
//
cell = row.createCell(cellNo++);
//
cell.setCellStyle(textStyle);
//
cell.setCellValue(rows.getLoanCount());
//
cell = row.createCell(cellNo++);
//
cell.setCellStyle(textStyle);
//
cell.setCellValue(rows.getDealDate());
//
cell = row.createCell(cellNo++);
//
cell.setCellStyle(textStyle);
//
cell.setCellValue(rows.getMessageResult());
//
cell = row.createCell(cellNo++);
//
cell.setCellStyle(textStyle);
//
cell.setCellValue(rows.getBranchName());
//
cell = row.createCell(cellNo++);
//
cell.setCellStyle(textStyle);
//
cell.setCellValue(rows.getInputBranchName());
}
//
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//
wb.write(byteArrayOutputStream);
byteArrayOutputStream.flush();
byteArrayOutputStream.close();
String nfsUrl = DictionaryUtil.transferSingleArgsName("LOAN_NFS_DATA", "nfsPath");
File file = new File(nfsUrl+userId+"/");
if(!file.exists()){
file.mkdir();
}
FileOutputStream fileOut = new FileOutputStream(new File(nfsUrl+userId+"/"+"SearchPhone.zip"));
ZipOutputStream zipOutputStream = new ZipOutputStream(fileOut);
ZipEntry entry = new ZipEntry("111.xls");
zipOutputStream.putNextEntry(entry);
byteArrayOutputStream.writeTo(zipOutputStream);
zipOutputStream.closeEntry();
byteArrayOutputStream.close(); //
zipOutputStream.close();
fileOut.close();
String nfsUrlPath = DictionaryUtil.transferSingleArgsName("LOAN_NFS_DATA", "urlNfsPath");
Runtime.getRuntime().exec("chmod 777 " + (nfsUrl+userId));
Runtime.getRuntime().exec("chmod 777 " + (nfsUrl+userId+"/"+"SearchPhone.zip"));
return nfsUrlPath+userId+"/"+"SearchPhone.zip";
}
//
private CellStyle bigTitle(Workbook wb){
CellStyle style = wb.createCellStyle();
Font font = wb.createFont();
font.setFontName(" ");
font.setFontHeightInPoints((short)16);
font.setBoldweight(Font.BOLDWEIGHT_BOLD); //
style.setFont(font);
style.setAlignment(CellStyle.ALIGN_CENTER); //
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); //
return style;
}
}
・