javaエクスポートexcelファイル(圧縮後にエクスポート)



 
  
                     excel    ,      ,                :    poi   2003  excel   
   
  

public File writeExcel(List> writeContent,String titleList[],String userId) {
		WritableWorkbook book = null;
  		File file = new File(nfsUrl+userId+"/"+"outSourceExport1.zip");//        zip 
		try {
			//     
			book = Workbook.createWorkbook(file);
			//   0       
			int linenum = 0;
			WritableSheet sheet = book.createSheet("      ", 0);
			for(int i=0;i columnMap = writeContent.get(i);
					Iterator iter = columnMap.entrySet().iterator();
					int y = 0;
					while (iter.hasNext()) {
						Map.Entry entry = (Map.Entry) iter.next();
						Object key = entry.getKey();
						Object val = entry.getValue();
						if(val!=null){sheet.addCell(new Label(y, linenum, val.toString()));}else{sheet.addCell(new Label(y, linenum, null));}
						y ++;
					}
					linenum ++;
				}
			}
			//          
			book.write();
		} catch (Exception e) {
			System.out.println(e);
		}finally{
			if(book!=null){
				try {
					book.close();
				} catch (Exception e) {
					e.printStackTrace();
				} 
			}
		}
		return file;
	}
public String  getFile(T_L000800 tL000800){
    
            //             ,       
			List> resultList = jdbcTemplate.queryForList(sqlColumnBuilder.toString());
			//       excel  (   resultList:        ,titleList excel  title,           ,    )
			File exportFile = writeExcel(resultList,titleList,tL000800.getTuserId());
			if(exportFile.exists()){
				//      ,     outputStream(          response,  response    ServletOutputStream out = response.getOutputStream()       )
				FileOutputStream out = new FileOutputStream(new File(nfsUrl+tL000800.getTuserId()+"/"+"outSourceExport.zip"));
				//                 
				FileInputStream  fis = new FileInputStream(exportFile);  
				//        ZipOutputStream 
				ZipOutputStream zipOutputStream = new ZipOutputStream(out);
				//    zip  ;  
				 ZipEntry zipEntry = new ZipEntry("    .xls");
				zipOutputStream.putNextEntry(zipEntry); 
				
				byte[] b = new byte[fis.available()];  
				//       ;  
				int len = 0;  
				while((len=fis.read(b)) >0){  
					zipOutputStream.write(b, 0, len);  
				}  
				//  ;  
				zipOutputStream.closeEntry();  
				zipOutputStream.close();
				if(fis != null){  
					fis.close();  
				}  
				out.flush();  
				out.close();
				//          ,     zip,      
				if(exportFile.getName().endsWith(".zip")){
					if(exportFile.delete()){
						System.out.println("  ");
					}else{
						System.out.println("  ");
					}
				}
				try {
			//         weblogic  ,              ,  777        
			Runtime.getRuntime().exec("chmod 777 " + (nfsUrl+tL000800.getTuserId()));
			Runtime.getRuntime().exec("chmod 777 " + (nfsUrl+tL000800.getTuserId()+"/"+"outSourceExport.zip"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return nfsUrlPath+tL000800.getTuserId()+"/"+"outSourceExport.zip";
	}


: , 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;
	}
}