자바 엑셀 내용 읽 기 코드 예제

3851 단어
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class ExcelExportTest {
	public final static String lineDlim="/###/";
	
	private static String getLine(HSSFSheet sheet, int row) {
		//       Sheet   
		HSSFRow rowline = sheet.getRow(row);
		//         
		StringBuffer buffer = new StringBuffer();
		int filledColumns = 0;
		//         
		if (rowline != null)
			filledColumns = rowline.getLastCellNum();
		HSSFCell cell = null;
		//        
		for (int i = 0; i < filledColumns; i++) {
			//     Cell
			cell = rowline.getCell((short) i);
			String cellvalue = null;
			if (cell != null) {
				//     Cell Type
				switch (cell.getCellType()) {
				//     Cell Type NUMERIC
				case HSSFCell.CELL_TYPE_NUMERIC: {
					//      cell   Date
					if (HSSFDateUtil.isCellDateFormatted(cell)) {
						//    Date   ,   Cell Date 
						Date date = cell.getDateCellValue();
						//  Date           
						cellvalue = (new SimpleDateFormat("yyyy-MM-dd")).format(date);
						//cellvalue = cell.getDateCellValue().toLocaleString();
					}
					//       
					else {
						//     Cell   
						Long num = new Long((long) cell
								.getNumericCellValue());
						cellvalue = String.valueOf(num);
						Double dn=cell.getNumericCellValue();
						if(dn==(double)num){
							cellvalue = String.valueOf(num);
						}else{
							DecimalFormat   numberForm   =  new DecimalFormat("0.############");   
						    cellvalue=   numberForm.format(dn);  
//							cellvalue=String.format("%1f", dn);
						}
					}
					break;
				}
					//     Cell Type STRIN
				case HSSFCell.CELL_TYPE_STRING:
					//      Cell   
					cellvalue = cell.getRichStringCellValue().getString().replaceAll("'", "''").trim();
					break;
				//    Cell 
				default:
					cellvalue = " ";
				}
			} else {
				cellvalue = "";
			}
			//             
			buffer.append(cellvalue).append("&&");
		}
		//            
		return buffer.toString();

	}
	
	public static void main(String[] args) throws IOException {
		InputStream in = new FileInputStream("D:/amember.xls");
	//	XLSUtil xu = new XLSUtil(in);
		//System.out.println(xu.getContent());
		HSSFWorkbook xslBook = null;

		int numOfSheets;

		xslBook = new HSSFWorkbook(in);
		numOfSheets = xslBook.getNumberOfSheets();
		
		StringBuffer buffer = new StringBuffer();
		HSSFSheet sheet;		

		for (int i = 0; i < numOfSheets; i++) {
			sheet = xslBook.getSheetAt(i);
			//        
			//sheet.getLastRowNum()
			for (int j = 0; j <= 5; j++) {
				buffer.append(getLine(sheet, j));
				buffer.append(lineDlim);
				System.out.println(buffer);
				buffer.setLength(0);
			}
			
			System.out.println("sheet>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
		}

	}

}

좋은 웹페이지 즐겨찾기