EXCEL 파일 쓰기

3710 단어
package excel;
import java.io.IOException; 
import java.io.OutputStream; 
import java.util.List; 

import jxl.Workbook; 
import jxl.write.Label; 
import jxl.write.WritableCellFormat; 
import jxl.write.WritableFont; 
import jxl.write.WritableSheet; 
import jxl.write.WritableWorkbook; 
import jxl.write.WriteException; 
import jxl.write.biff.RowsExceededException; 

public class JxlExcelWriter { 
    public void writeExcel(OutputStream out, List datas, String[] title) { 
        if(datas == null) { 
            throw new IllegalArgumentException(" excel List !"); 
        } 
        try { 
        WritableWorkbook workbook = Workbook.createWorkbook(out); 
        WritableSheet ws = workbook.createSheet("sheet 1", 0); 
        int rowNum = 0; 
        if(title != null) { 
        //putRow(ws, 0, title);//  
        WritableFont wf = new WritableFont(WritableFont.TIMES,10,WritableFont.BOLD,false,jxl.format.UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.RED);
        WritableCellFormat wcf = new WritableCellFormat(wf); 
        for(int i=0;i<title.length;i++) { 
        Label cell = new Label(i,0,title[i],wcf);        
        ws.addCell(cell); 
        } 
                rowNum = 1; 
        } 
        for(int i=0;i<datas.size();i++,rowNum++) { 
        Object[] cells = (Object[])datas.get(i); 
        putRow(ws,rowNum,cells); 
        } 
        workbook.write(); 
        workbook.close(); 
        }catch(RowsExceededException e) { 
        System.out.println("jxl write RowsExceededException: " + e.getMessage()); 
        }catch(WriteException e) { 
        System.out.println("jxl write WriteException: " + e.getMessage()); 
        }catch(IOException e) { 
        System.out.println("jxl write file i/o exception!, cause by: " + e.getMessage()); 
        } 
    }        
    private void putRow(WritableSheet ws, int rowNum, Object[] cells) throws RowsExceededException, WriteException { 
	    for(int j=0;j<cells.length;j++) { 
		    Label cell = new Label(j,rowNum,""+cells[j]); 
		    ws.addCell(cell); 
	    } 
    } 
} 



package excel;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

public class WriteToExcel {
	public void testWriteExcel() {
		List datas = new ArrayList();
		String[] data = { " ", "XXX ", "http://www.xxxx.com/", " ", "2015-11-04" };
		datas.add(data);
		datas.add(data);
		datas.add(data);
		datas.add(data);
		datas.add(data);
		datas.add(data);
		datas.add(data);
		String[] title = { " ", " ", " ", " ", " "};
		try {
			OutputStream out = new FileOutputStream(new File("f:/my.xls"));
			JxlExcelWriter jxlExcelWriter = new JxlExcelWriter();
			jxlExcelWriter.writeExcel(out, datas, title);
			
			out.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		WriteToExcel wte = new WriteToExcel();
		wte.testWriteExcel();
	}
}

좋은 웹페이지 즐겨찾기