Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- import org.apache.poi.ss.usermodel.Cell;
- import org.apache.poi.ss.usermodel.Row;
- import org.apache.poi.ss.usermodel.Sheet;
- import org.apache.poi.ss.usermodel.Workbook;
- import java.io.ByteArrayOutputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.OutputStreamWriter;
- import java.nio.charset.Charset;
- import java.nio.charset.StandardCharsets;
- /**
- * Add this to your pom for apache poi
- *
- * <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
- * <dependency>
- * <groupId>org.apache.poi</groupId>
- * <artifactId>poi</artifactId>
- * <version>4.0.0</version>
- * </dependency>
- *
- * ONly if you need ".xlsX"
- * <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
- * <dependency>
- * <groupId>org.apache.poi</groupId>
- * <artifactId>poi-ooxml</artifactId>
- * <version>4.0.0</version>
- * </dependency>
- *
- */
- public class ExcelEncoding {
- public static void main(String[] args) throws Exception {
- ApachePoiExcelEncoding excel1Encoded = new ApachePoiExcelEncoding("sheet 1");
- excel1Encoded.writeData();
- excel1Encoded.save("encoded_sheet_iso_8859-15_latin_9");
- ApachePoiExcelEncoding excel2Encoded = new ApachePoiExcelEncoding("sheet test 2", StandardCharsets.ISO_8859_1.name());
- excel2Encoded.writeData();
- excel2Encoded.save("encoded_sheet_ISO-8859-1");
- }
- /**
- * Write an excel with a custom encoding
- */
- static class ApachePoiExcelEncoding {
- private String encoding = "ISO-8859-15";
- private Workbook wb;
- private Sheet sheet;
- ApachePoiExcelEncoding(String sheetName) {
- wb = new HSSFWorkbook();
- sheet = wb.createSheet(sheetName);
- }
- public ApachePoiExcelEncoding(String sheetName, String encoding) {
- this.wb = new HSSFWorkbook();
- this.sheet = wb.createSheet(sheetName);
- this.encoding = encoding;
- }
- void writeData() {
- // Create a row and put some cells in it. Rows are 0 based.
- Row row = sheet.createRow(0);
- Cell cell = row.createCell((short) 0);
- cell.setCellValue(14654);
- //plain string value
- row.createCell(2).setCellValue("This is a string cell");
- //formula
- row.createCell(5).setCellFormula("SUM(A2:B2)");
- row.createCell(6).setCellFormula("HYPERLINK(\"http://google.ch\",\"Google CH\")");
- }
- void save(final String src) throws IOException {
- String fullPath = getPathWithExtension(src);
- // Invoking HSSFWorkbook.getBytes() does not return all the data
- // necessary to re- construct a complete Excel file
- // We need to use write method with the ByteArrayOutputStream to get at the byte array
- ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
- wb.write(byteOut);
- // get the file in string
- String fileString = new String(byteOut.toByteArray(), Charset.forName(encoding));
- //write the file using stream writer
- try (OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(fullPath), encoding)) {
- outputStreamWriter.write(fileString);
- }
- byteOut.close();
- }
- private String getPathWithExtension(String src) {
- return !src.endsWith(".xls") ? src + ".xls" : src;
- }
- }
- }
Add Comment
Please, Sign In to add comment