主页 > 办公自动化 > java中poi怎么生成excel

java中poi怎么生成excel

2022-10-10 12:09来源:m.sf1369.com作者:宇宇

一、java中poi怎么生成excel

网上好多啊

我给你找了一个例子

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;

}

二、如何利用jakarta poi api 创建excel 文档

Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft Office格式档案读和写的功能。 结构: HSSF - 提供读写Microsoft Excel格式档案的功能。 XSSF - 提供读写Microsoft Excel OOXML格式档案的功能。

三、Java POI生成excel怎么进行打印页面设置?

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 ); // 右边距

相关推荐

企业办公软件都有哪些?

办公自动化 2024-01-14

数控车床自动编程用什么软件?

办公自动化 2024-01-12

写字间物业管理方案

办公自动化 2023-12-09

自动化的好处和坏处

办公自动化 2023-12-05

单片机有什么特征?

办公自动化 2023-11-27

excel2007教程|excel2007教程下载

办公自动化 2023-11-22