电脑故障

位置:IT落伍者 >> 电脑故障 >> 浏览文章

jxl 实用技巧(2)


发布日期:2020/7/22
 

// 数字的公式计算

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());

}

}

}

上一篇:消除对过期对象的引用

下一篇:函数与自变量的亲密关系