forked from ltearno/formations
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
106 additions
and
131 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
...ts/javaee/movie-db/src/main/java/fr/lteconsulting/training/moviedb/outil/ExportExcel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package fr.lteconsulting.training.moviedb.outil; | ||
|
||
import org.apache.poi.hssf.usermodel.HSSFDataFormat; | ||
import org.apache.poi.hssf.usermodel.HSSFWorkbook; | ||
import org.apache.poi.ss.usermodel.*; | ||
|
||
import javax.servlet.ServletOutputStream; | ||
import java.io.IOException; | ||
import java.util.Date; | ||
|
||
public class ExportExcel { | ||
private Workbook workbook; | ||
private Sheet sheet; | ||
|
||
private final CellStyle intCellStyle; | ||
private final CellStyle doubleCellStyle; | ||
private final CellStyle textCellStyle; | ||
|
||
private int currentLine = 0; | ||
|
||
public ExportExcel() { | ||
workbook = new HSSFWorkbook(); | ||
sheet = workbook.createSheet(); | ||
|
||
workbook.setSheetName(0, "Movie Database export " + new Date()); | ||
|
||
DataFormat df = workbook.createDataFormat(); | ||
doubleCellStyle = workbook.createCellStyle(); | ||
doubleCellStyle.setDataFormat(df.getFormat("#,##0.0")); | ||
|
||
intCellStyle = workbook.createCellStyle(); | ||
intCellStyle.setDataFormat(df.getFormat("#")); | ||
|
||
textCellStyle = workbook.createCellStyle(); | ||
textCellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("text")); | ||
} | ||
|
||
public void line(Object... cellValues) { | ||
Row row = sheet.createRow(currentLine++); | ||
|
||
for (int c = 0; c < cellValues.length; c++) { | ||
Object cellValue = cellValues[c]; | ||
|
||
Cell cell = row.createCell(c); | ||
cell.setCellValue(cellValue.toString()); | ||
|
||
if (cellValue instanceof String) | ||
cell.setCellStyle(textCellStyle); | ||
else if (cellValue instanceof Integer) | ||
cell.setCellStyle(intCellStyle); | ||
else if (cellValue instanceof Double) | ||
cell.setCellStyle(doubleCellStyle); | ||
} | ||
} | ||
|
||
public void write(ServletOutputStream outputStream) throws IOException { | ||
workbook.write(outputStream); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters