자바,poi 로 엑셀 내 보 내기

3848 단어 자바poiExcel

package zy.demo.test;

import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.util.Region;
import org.hibernate.mapping.Array;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ToExcel {
	public static String xlsFile = "d:/test.xls"; //    Excel     

	//   cell            
	@SuppressWarnings("deprecation")
	public static void main(String[] agrs) throws IOException {
		HSSFWorkbook workbook = new HSSFWorkbook(); //        
		HSSFSheet sheet = workbook.createSheet("     "); //        
		//             firstSheet
		//           ,       UTF_16
		workbook.setSheetName(0, "firstSheet");
		//     ,  8 
		HSSFRow row = sheet.createRow((short) 0);
		HSSFRow row1 = sheet.createRow((short) 1);
		//         
		sheet.setColumnWidth((short) 0, (short) 4000);
		sheet.setColumnWidth((short) 1, (short) 4000);
		sheet.setColumnWidth((short) 2, (short) 4000);
		sheet.setColumnWidth((short) 3, (short) 4000);
		sheet.setColumnWidth((short) 4, (short) 4000);
		sheet.setColumnWidth((short) 5, (short) 4000);
		sheet.setColumnWidth((short) 6, (short) 5000);
		sheet.setColumnWidth((short) 7, (short) 5000);
		sheet.setColumnWidth((short) 8, (short) 5000);
		//    
		HSSFCell cell1 = row1.createCell((short) 0);
		//             
		cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
		//             ,       UTF_16。.....

		//             
		cell1.setCellValue("   ");
		String[] head = { "  ", " 1", " 2", " 3", " 4", " 5", " 6", " 7", " 8" };
		for (int i = 0; i < head.length; i++) {
			HSSFCell cell = row.createCell((short) i);
			cell.setCellType(HSSFCell.CELL_TYPE_STRING);
			cell.setCellValue(head[i]);
		}
		ArrayList<String> list = new ArrayList();
		list.add("1");
		list.add("2");
		list.add("3");
		list.add("4");
		for(int i=0;i<list.size();i++){
			//   
			HSSFRow row2 = sheet.createRow((short)(i+2));
			//   
			HSSFCell cell = row2.createCell((short)0);
			cell.setCellValue(list.get(0));
		}
		FileOutputStream fOut = new FileOutputStream(xlsFile);
		;
		try {
			fOut = new FileOutputStream(xlsFile);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		workbook.write(fOut);
		fOut.flush();
		System.out.println("    ...");
		//          Excel    
		FileInputStream fIn = new FileInputStream(xlsFile);
		HSSFWorkbook readWorkBook = new HSSFWorkbook(fIn);
		HSSFSheet readSheet = readWorkBook.getSheet("firstSheet");
		HSSFRow readRow = readSheet.getRow(0);
		HSSFCell readCell = readRow.getCell((short) 0);
		HSSFCell readCell1 = readRow.getCell((short) 1);
		System.out.println("      :" + readCell.getStringCellValue());
		System.out.println(readCell1.getStringCellValue());
	}
}

좋은 웹페이지 즐겨찾기