// 數字的公式計算
Number n = new jxlwriteNumber( );// A
sheetaddCell(n);
n = new Number( );// B
sheetaddCell(n);
NumberFormat dp = new NumberFormat(####); // 設置單元格裡面的數字格式
WritableCellFormat dpcell = new WritableCellFormat(dp);
dpcellsetWrap(true);
Formula f = new Formula( (a+b)/ dpcell); // 設置C公式
sheetaddCell(f);
f = new Formula( SUM(A:B) dpcell);// 設置D公式
sheetaddCell(f);
// 設置sheet的樣式
sheetgetSettings()setProtected(true); // 設置xls的保護單元格為只讀的
sheetgetSettings()setPassword(); // 設置xls的密碼
sheetgetSettings()setDefaultColumnWidth(); // 設置列的默認寬度cm左右
sheetsetRowView( );// 設置第行高度
sheetsetRowView( false);// 這樣可以自動把行高擴展
sheetsetColumnView( );// 設置第列寬度cm左右
rgeCells( );// 合並單元格合並AB也就是列行 與 列行之間的矩形
// 設置邊框
drawRect(sheet BorderLineStyleTHICK ColourBLACK null);
rgeCells( );
wwbwrite();
wwbclose();
osclose();
}
public static void drawRect(WritableSheet sheet int x int y int width
int height BorderLineStyle style Colour BorderColor
Colour bgColor) throws WriteException {
for (int w = ; w < width; w++) {
for (int h = ; h < height; h++) {
WritableCellFormat alignStyle = new WritableCellFormat(); // 單元格樣式
alignStylesetAlignment(AlignmentCENTRE); // 設置對齊方式
alignStylesetVerticalAlignment(VerticalAlignmentCENTRE);// 設置對齊方式
if (h == )// 畫上
alignStylesetBorder(BorderTOP style BorderColor);// 設置邊框的顏色和樣式
if (w == )// 畫左
alignStylesetBorder(BorderLEFT style BorderColor);// 設置邊框的顏色和樣式
if (w == width )// 畫右
alignStylesetBorder(BorderRIGHT style BorderColor);// 設置邊框的顏色和樣式
if (h == height )// 畫下
alignStylesetBorder(BorderBOTTOM style BorderColor);// 設置邊框的顏色和樣式
// drawLine(sheet x y BorderBOTTOM);
if (bgColor != null)
alignStylesetBackground(bgColor); // 背靜色
Label mergelabel = new Label(x y alignStyle);
// topleftXIndex topleftYIndex bottomRightXIndex
// bottomRightYIndex
// rgeCells( );
sheetaddCell(mergelabel);
y++;
}
y = height;
x++;
}
}
public static ArrayList<String> sampleReadExcel(File inputFile
int inputFileSheetIndex) throws Exception {
ArrayList<String> list = new ArrayList<String>();
Workbook book = null;
Cell cell = null;
// 避免亂碼的設置
WorkbookSettings setting = new WorkbookSettings();
javautilLocale locale = new javautilLocale(zh CN);
settingsetLocale(locale);
settingsetEncoding(ISO);
book = WorkbookgetWorkbook(inputFile setting);
Sheet sheet = bookgetSheet(inputFileSheetIndex);
for (int rowIndex = ; rowIndex < sheetgetRows(); rowIndex++) {// Excel第一行為表頭因此J初值設為
for (int colIndex = ; colIndex < sheetgetColumns(); colIndex++) {// 只需從Excel中取出列
cell = sheetgetCell(colIndex rowIndex);
listadd(cellgetContents());
}
}
// 【問題如果在實際部署的時候沒有寫下面這句是否會導致不斷消耗掉服務器的內存?jxl裡面有個ReadWritejava沒有關閉讀的只關閉了寫的】
bookclose();
return list;
}
public static void setCellValueDirectly(WritableSheet sheet Cell cell
Object newValue CellType type) throws Exception {
if (type == CellTypeDATE || type == CellTypeDATE_FORMULA) {
sheetaddCell(new jxlwriteDateTime(cellgetColumn() cell
getRow() (Date) newValue
new jxlwriteWritableCellFormat(cellgetCellFormat())));
} else if (type == CellTypeNUMBER || type == CellTypeNUMBER_FORMULA) {
sheetaddCell(new jxlwriteNumber(cellgetColumn() cellgetRow()
((Double) newValue)doublue()
new jxlwriteWritableCellFormat(cellgetCellFormat())));
} else if (type == CellTypeLABEL || type == CellTypeSTRING_FORMULA) {
sheetaddCell(new Label(cellgetColumn() cellgetRow()
(String) newValue new jxlwriteWritableCellFormat(cell
getCellFormat())));
} else {
throw new Exception(不支持的其它單元格類型 + type);
// Systemerrprintln(不支持的其它單元格類型 + cellgetType() + at +
// cellgetColumn() + + cellgetRow() + current content: +
// cellgetContents());
}
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25570.html