Sourabh Dubey
Sourabh Dubey

Reputation: 406

How to convert XLSX file to CSV with font formats in java

I'm trying to convert my xls file to csv file. my java code convert text properly. but i'm not getting any font formatting and other formatting. so how can i get my font formatting in my CSV(open in excel) file.

public class XlsxtoCSV {

        public static void main(String[] args) throws Exception{
             FileInputStream input_document = new FileInputStream(new File("/home/blackpearl/Downloads/file.xlsx"));
             HSSFWorkbook my_xls_workbook = new HSSFWorkbook(input_document); 
             HSSFSheet  my_worksheet = my_xls_workbook.getSheetAt(0); 

             Iterator<Row> rowIterator = my_worksheet.iterator();
             FileWriter my_csv=new FileWriter("/home/blackpearl/Downloads/Newfile.csv");
             CSVWriter my_csv_output=new CSVWriter(my_csv); 
             while(rowIterator.hasNext()) {
                     Row row = rowIterator.next(); 
                     int i=0;//String array
                     String[] csvdata = new String[20];
                     Iterator<Cell> cellIterator = row.cellIterator();
                             while(cellIterator.hasNext()) {
                                     Cell cell = cellIterator.next(); //Fetch CELL
                                     switch(cell.getCellType()) { //Identify CELL type
                                     case Cell.CELL_TYPE_STRING:
                                             csvdata[i]= cell.getStringCellValue();                                              
                                             break;
                                     }
                                     i=i+1;
                             }
             my_csv_output.writeNext(csvdata);
             }
             System.out.println("file imported");
             my_csv_output.close(); //close the CSV file
             input_document.close(); //close xlsx file
     }
}

Upvotes: 0

Views: 388

Answers (1)

mic.sca
mic.sca

Reputation: 1696

CSV stands for comma-separated values. And it really is simply that. It’s text, with values separated by commas (or, in some versions, semi-colons or other delimiters). It’s basically a plain text file, and can be opened and edited with a text editor. It is not possible to include formatting information in that file format.

Upvotes: 2

Related Questions