1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
try {
HSSFWorkbook fWorkbook = new HSSFWorkbook();
HSSFSheet fSheet;
fSheet = fWorkbook.createSheet("new Sheet");
HSSFFont sheetTitleFont = fWorkbook.createFont();
File file = new File("C:\\Users\\tosiba\\Desktop\\projet\\Nabila\\liste.xls");
HSSFCellStyle cellStyle = fWorkbook.createCellStyle();
sheetTitleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//sheetTitleFont.setColor();
TableModel model = table.getModel();
TableColumnModel model1 = table.getTableHeader().getColumnModel();
HSSFRow fRow1 = fSheet.createRow((short) 0);
for (int i = 0; i < model1.getColumnCount(); i++){
HSSFCell cell = fRow1.createCell((short) i);
cell.setCellValue(model1.getColumn(i).getHeaderValue().toString());
}
for (int i = 1; i < model.getRowCount(); i++) {
HSSFRow fRow = fSheet.createRow((short) i);
for (int j = 0; j < model.getColumnCount(); j++) {
HSSFCell cell = fRow.createCell((short) j);
cell.setCellValue(Objects.toString(model.getValueAt(i, j), ""));
cell.setCellStyle(cellStyle);
}
}
FileOutputStream fileOutputStream;
fileOutputStream = new FileOutputStream(file);
try (BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream)) {
fWorkbook.write(bos);
}
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace(); |