jxl 操作 excel

package cn.pbc.lltj.report.util;

import java.io.*;

import jxl.*;
import jxl.format.CellFormat;
import jxl.write.*;
import jxl.write.Number;
import jxl.write.biff.RowsExceededException;

/**
* 封裝Excel模板和報表的操作
*
* @author zgw
*
*/
public class Excel {
private WritableWorkbook wwb;
private WritableSheet currentSheet;
private WritableCellFormat wcfNumber;
private WritableCellFormat wcfLabel;
private WritableCellFormat wcfTitle;

/**
* 構造函數,讀取模板創建Excel報表,currentSheet默認指向sheet0
*
* @param template Excel模版文檔
* @param out Excel報表文件名
* @throws ExcelException
*/
public Excel(String template,String out) throws ExcelException {
InputStream ins;
Workbook sourcebook;
try {
ins = new FileInputStream(template);
} catch (FileNotFoundException e) {
throw new ExcelException("找不到Excel模版文件", e);
}

try {
sourcebook = Workbook.getWorkbook(ins);
} catch (Exception e) {
throw new ExcelException("Excel模版文件讀取錯誤", e);
}

File outFile = new File(out);
try {
wwb = Workbook.createWorkbook(outFile, sourcebook);
NumberFormat nf = new NumberFormat("#0.00");
    wcfNumber = new WritableCellFormat(nf);
    wcfNumber.setBorder(Border.ALL, BorderLineStyle.THIN);
// 把水平對齊方式指定爲居中
    wcfNumber.setAlignment(jxl.format.Alignment.CENTRE);
// 把垂直對齊方式指定爲居中
    wcfNumber.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
    // 設置自適應大小
    wcfNumber.setShrinkToFit(true);
   
    wcfLabel = new WritableCellFormat();
    wcfLabel.setBorder(Border.ALL, BorderLineStyle.THIN);

    wcfLabel.setVerticalAlignment(VerticalAlignment.CENTRE);
    wcfLabel.setShrinkToFit(true);
   
    WritableFont wf = new WritableFont(WritableFont.ARIAL, 15, WritableFont.BOLD, false);
    wcfTitle = new WritableCellFormat(wf);;
    wcfTitle.setAlignment(jxl.format.Alignment.CENTRE);
    wcfTitle.setVerticalAlignment(VerticalAlignment.CENTRE);
} catch (IOException e) {
throw new ExcelException("創建Excel報表出錯", e);
} catch (WriteException e) {
throw new ExcelException("設置Excel單元格錯誤",e);
}
currentSheet = this.wwb.getSheet(0);
}

/**
* 在報表中插入一個數字,格式不變
*
* @param col 列 從0開始
* @param row 行 從0開始
* @param value 要插入的值
* @throws ExcelException
*/
public void insert(int col, int row, double value) throws ExcelException{
Number num = new Number(col,row,value,wcfNumber);
try {
this.currentSheet.addCell(num);
} catch (RowsExceededException e) {
throw new ExcelException("行數溢出",e);
} catch (WriteException e) {
throw new ExcelException("Excel報表寫入錯誤",e);
} catch (IndexOutOfBoundsException e) {
throw new ExcelException("Sheet溢出,超出sheet個數",e);
}
}

/**
* 在報表中插入一個字符串,格式不變
*
* @param col 列 從0開始
* @param row 行 從0開始
* @param value 要插入的值
* @throws ExcelException
*/
public void insert(int col, int row, String value) throws ExcelException{
Label label = new Label(col,row,value,wcfLabel);
try {
this.currentSheet.addCell(label);
} catch (RowsExceededException e) {
throw new ExcelException("行數溢出",e);
} catch (WriteException e) {
throw new ExcelException("Excel報表寫入錯誤",e);
} catch (IndexOutOfBoundsException e) {
throw new ExcelException("Sheet溢出,超出sheet個數",e);
}
}

public void insertTitle(int col,int row,String title)throws ExcelException{
Label label = new Label(col,row,title,wcfTitle);
try {
this.currentSheet.addCell(label);
} catch (RowsExceededException e) {
throw new ExcelException("行數溢出",e);
} catch (WriteException e) {
throw new ExcelException("Excel報表寫入錯誤",e);
} catch (IndexOutOfBoundsException e) {
throw new ExcelException("Sheet溢出,超出sheet個數",e);
}
}

/**
* 返回單元格的值
* @param col
* @param row
* @return
*/
public String getCellContent(int col,int row){
return this.currentSheet.getCell(col, row).getContents();
}

public void merge(int col1, int row1, int col2, int row2) throws ExcelException{

try {
this.currentSheet.mergeCells(col1, row1, col2, row2);
} catch (RowsExceededException e) {
throw new ExcelException("Excel合併單元格異常",e);
} catch (WriteException e) {
throw new ExcelException("Excel合併單元格異常",e);
}
}

/**
* 寫入並關閉報表
* 1.修改表報後必須關閉,否則不能能寫入文件中
* 2.報表關閉後不能再操作
*
* @throws ExcelException
*/
public void close() throws ExcelException{
try {
wwb.write();
wwb.close();
} catch (Exception e) {
throw new ExcelException("寫入Excel錯誤", e);
}
}

/**
* 合併col列上相同的單元格
*
* @param col 從0開始的列號
* @throws ExcelException
*/
public void mergeSameOnColumn(int col,int endrow) throws ExcelException{
try {
Cell[] cells = this.currentSheet.getColumn(col);
int i = 0;
while(i<cells.length&&i<endrow){
int j = i;
String value = cells[i].getContents().trim();
while(j<cells.length&&cells[j].getContents().trim().equals(value)){
j++;
}
if(i!=j-1){
this.currentSheet.mergeCells(col, i, col, j-1);
this.insert(col, i, value);
i=j;
}else{
i++;
}

}
} catch (RowsExceededException e) {
throw new ExcelException("行數溢出",e);
} catch (WriteException e) {
throw new ExcelException("Excel報表寫入錯誤",e);
}
}

/**
* 設置當前sheet
* @param sheet
*/
public void setCurrentSheet(int sheet){
this.currentSheet = this.wwb.getSheet(sheet);
}

/**
* 測試
*
*/
public static void main(String args[]) {
try {
String inFile = "d:/source.xls";
String outFile = "d:/test.xls";
Excel xls = new Excel(inFile, outFile);
int i=5;
while(i++<10){
xls.insert(1, i, 23.53*i);
}
xls.mergeSameOnColumn(0,10);
xls.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章