Posted on 2006-12-18 10:36
码农cz 阅读(605)
评论(0) 编辑 收藏 所属分类:
As u
1、遍历workbook
代码
-
-
POIFSFileSystem fs =
new
POIFSFileSystem(
new
FileInputStream(filePath));
-
HSSFWorkbook wb =
new
HSSFWorkbook(fs);
-
for
(
int
i =
0
; i < wb.getNumberOfSheets(); i++) {
-
HSSFSheet sheet = wb.getSheetAt(i);
-
for
(
int
i = sheet.getFirstRowNum(); i < sheet.getLastRowNum(); i ++) {
-
HSSFRow row = sheet.getRow(i);
-
if
(row !=
null
) {
-
。。。操作}
-
}
-
}
-
-
FileOutputStream fos =
new
FileOutputStream(objectPath);
-
-
swb.write(fos);
-
fos.close();
2、得到列和单元格
代码
-
HSSFRow row = sheet.getRow(i);
-
HSSFCell cell = row.getCell((
short
) j);
3、设置sheet名称和单元格内容为中文
代码
-
wb.setSheetName(n,
"中文"
,HSSFCell.ENCODING_UTF_16);
-
cell.setEncoding((
short
)
1
);
-
cell.setCellValue(
"中文"
);
4、单元格内容未公式或数值,可以这样读写
代码
-
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
-
cell.getNumericCellValue()
5、设置列宽、行高
代码
-
sheet.setColumnWidth((
short
)column,(
short
)width);
-
row.setHeight((
short
)height);
6、添加区域,合并单元格
代码
-
Region region =
new
Region((
short
)rowFrom,(
short
)columnFrom,(
short
)rowTo,(
short
)columnTo);
-
sheet.addMergedRegion(region);
-
-
sheet.getNumMergedRegions()
7、常用方法
根据单元格不同属性返回字符串数值
代码
-
public
String getCellStringValue(HSSFCell cell) {
-
String cellValue =
""
;
-
switch
(cell.getCellType()) {
-
case
HSSFCell.CELL_TYPE_STRING:
-
cellValue = cell.getStringCellValue();
-
if
(cellValue.trim().equals(
""
)||cellValue.trim().length()<=
0
)
-
cellValue=
" "
;
-
break
;
-
case
HSSFCell.CELL_TYPE_NUMERIC:
-
cellValue = String.valueOf(cell.getNumericCellValue());
-
break
;
-
case
HSSFCell.CELL_TYPE_FORMULA:
-
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
-
cellValue = String.valueOf(cell.getNumericCellValue());
-
break
;
-
case
HSSFCell.CELL_TYPE_BLANK:
-
cellValue=
" "
;
-
break
;
-
case
HSSFCell.CELL_TYPE_BOOLEAN:
-
break
;
-
case
HSSFCell.CELL_TYPE_ERROR:
-
break
;
-
default
:
-
break
;
-
}
-
return
cellValue;
-
}
8、常用单元格边框格式
虚线HSSFCellStyle.BORDER_DOTTED 实线HSSFCellStyle.BORDER_THIN
代码
-
public
static
HSSFCellStyle getCellStyle(
short
type)
-
{
-
HSSFWorkbook wb =
new
HSSFWorkbook();
-
HSSFCellStyle style = wb.createCellStyle();
-
style.setBorderBottom(type);
-
style.setBorderLeft(type);
-
style.setBorderRight(type);
-
style.setBorderTop(type);
-
return
style;
-
}
9、设置字体和内容位置
代码
-
HSSFFont f = wb.createFont();
-
f.setFontHeightInPoints((
short
)
11
);
-
f.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
-
style.setFont(f);
-
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
-
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
-
style.setRotation(
short
rotation);
-
HSSFDataFormat df = wb.createDataFormat();
-
style1.setDataFormat(df.getFormat(
"0.00%"
));
-
cell.setCellFormula(string);
-
style.setRotation(
short
rotation);
-
cell.setCellStyle(style);
10、插入图片
论坛里看到的
代码 -
- ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
- BufferedImage bufferImg = ImageIO.read(new File("ok.jpg"));
- ImageIO.write(bufferImg,"jpg",byteArrayOut);
-
- FileInputStream fos = new FileInputStream(filePathName+"/stencil.xlt");
- fs = new POIFSFileSystem(fos);
-
- HSSFWorkbook wb = new HSSFWorkbook(fs);
- HSSFSheet sheet = wb.getSheetAt(0);
- HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
- HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,1023,255,(short) 0,0,(short)10,10);
- patriarch.createPicture(anchor , wb.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
|
最后更新:2006-10-30 20:12