2022-10-10 12:09来源:m.sf1369.com作者:宇宇
网上好多啊
我给你找了一个例子
view plaincopy to clipboardprint?
public static void main(String[] args) {
try {
String filepath = d:\\问题清单.xls;
FileInputStream fis = new FileInputStream(filepath);
// POIFSFileSystem pfs = new POIFSFileSystem(new FileInputStream(d:\\OA问题清单.xls));
// HSSFWorkbook hwb = new HSSFWorkbook(pfs);
HSSFWorkbook hwb = new HSSFWorkbook(fis);
// HSSFSheet hws = hwb.getSheetAt(0);
HSSFSheet hws = hwb.getSheet(问题清单);
HSSFRow row = hws.getRow(2);
HSSFCell cell = null;
cell = row.getCell((short) 1);
System.out.println(cellnumber:+cell.getCellNum());
System.out.println(cellvalue:+getExcelCellValue(cell));
cell.setCellValue(new Date());
cell = row.getCell((short)2);
HSSFRichTextString rts = new HSSFRichTextString(输入);
cell.setCellValue(rts);
FileOutputStream fos = new FileOutputStream(filepath);
hwb.write(fos);
fis.close();
fos.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(poi.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(poi.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static String getExcelCellValue(HSSFCell cell) {
String ret = ;
SimpleDateFormat sdf = new SimpleDateFormat(yyyy-MM-dd HH:mm:ss);
DecimalFormat df = new DecimalFormat(#);
int celltype = cell.getCellType();
switch(celltype){
case HSSFCell.CELL_TYPE_NUMERIC:
if(HSSFDateUtil.isCellDateFormatted(cell)){
ret = sdf.format(cell.getDateCellValue());
}else{
ret = df.format(cell.getNumericCellValue());
}
break;
case HSSFCell.CELL_TYPE_STRING:
ret = cell.getRichStringCellValue().toString();
break;
default:
}
return ret;
}
Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft Office格式档案读和写的功能。 结构: HSSF - 提供读写Microsoft Excel格式档案的功能。 XSSF - 提供读写Microsoft Excel OOXML格式档案的功能。
HSSFPrintSetup printSetup = sheet.getPrintSetup();
printSetup.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE); // 纸张
sheet.setDisplayGridlines(false);
sheet.setPrintGridlines(false);
sheet.setMargin(HSSFSheet.TopMargin,( double ) 0.2 ); // 上边距
sheet.setMargin(HSSFSheet.BottomMargin,( double ) 0.2 ); // 下边距
sheet.setMargin(HSSFSheet.LeftMargin,( double ) 0.2 ); // 左边距
sheet.setMargin(HSSFSheet.RightMargin,( double ) 0.2 ); // 右边距