java生成一个Excel文件
HSSFWorkbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet();
OutputStream out = null;
try {
String path = "C:\\Users\\12449\\Desktop\\测试.xls";
out = new FileOutputStream(path);
workbook.write(out);
} catch (IOException e) {
//waitingDialog.setVisible(false);
// TODO Auto-generated catch block
e.printStackTrace();
try {
out.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
throw new RuntimeException(e1.getMessage());
}
}finally{
try {
out.close();
} catch (IOException e) {
//waitingDialog.setVisible(false);
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
}
注意:这个的excel文件后缀要是.xls,如果是.xlsx,在打开的时候会报格式错误!

浙公网安备 33010602011771号