xlsx to csv converter Java with apache poi - java

My question is based on this Question. This is my build so far:
package converters;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
class XlsxtoCSV {
public static void main(String[] args) {
File inputFile = new File(
"C:\\Users\\USR\\Desktop\\testExport.xlsx");
File outputFile = new File("C:\\Users\\USR\\Desktop\\output.csv");
xlsx(inputFile, outputFile);
}
static void xlsx(File inputFile, File outputFile) {
String seperator = ";";
// For storing data into CSV files
StringBuffer data = new StringBuffer();
try {
FileOutputStream fos = new FileOutputStream(outputFile);
// Get the workbook object for XLSX file
XSSFWorkbook wBook = new XSSFWorkbook(
new FileInputStream(inputFile));
// Get first sheet from the workbook
XSSFSheet sheet = wBook.getSheetAt(0);
Row row;
Cell cell;
// Iterate through each rows from first sheet
Iterator<Row> rowIterator = sheet.iterator();
int i = 0; //I need 11 columns which can be empty
while (i < 11) {
row = rowIterator.next();
// For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
data.append(cell.getBooleanCellValue() + seperator);
break;
case Cell.CELL_TYPE_NUMERIC:
data.append(cell.getNumericCellValue() + seperator);
break;
case Cell.CELL_TYPE_STRING:
data.append(cell.getStringCellValue() + seperator);
break;
case Cell.CELL_TYPE_BLANK:
data.append("" + seperator);
break;
default:
data.append(cell + seperator);
}
}
data.append("\r\n");
i++;
}
fos.write(data.toString().getBytes());
fos.close();
System.out.println(data.toString());
} catch (Exception ioe) {
ioe.printStackTrace();
}
}
}
but my problem is that it won't work properly if there is an line break in a cell, which is important for me.
I need it to be exactly like when you "save as csv" in Excel.

Related

How to read from merged cells of Excel in Java using Apache POI?

I have a Excel file in .xlsx format. I have stored data by merging cells to form various columns. I am reading the Excel file via a Java web application and saving its data to a database (MySQL). But when I read from merged cells I get null values along with what are stored in the columns as well as the headers. I am using Apache POI. My code is:
public static void excelToDBLogIN() {
FileInputStream file = null;
Boolean flag = true;
ArrayList<String> rows = new ArrayList<String>();
try {
// here uploadFolder contains the path to the Login 3.xlsx file
file = new FileInputStream(new File(uploadFolder + "Login 3.xlsx"));
//Create Workbook instance holding reference to .xlsx file
XSSFWorkbook workbook = new XSSFWorkbook(file);
//Get first/desired sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);
//Iterate through each rows one by one
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
//For each row, iterate through all the columns
Iterator<Cell> cellIterator = row.cellIterator();
String tuple = "";
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
//Check the cell type and format accordingly
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
//int value = new BigDecimal(cell.getNumericCellValue()).setScale(0, RoundingMode.HALF_UP).intValue();
//tuple = tuple + String.valueOf(value) + "+";
DataFormatter objDefaultFormat = new DataFormatter();
String str = objDefaultFormat.formatCellValue(cell);
tuple = tuple + str + "+";
break;
case Cell.CELL_TYPE_STRING:
tuple = tuple + cell.getStringCellValue() + "+";
break;
case Cell.CELL_TYPE_BLANK:
tuple = tuple + "" + "+";
break;
}
}
rows.add(tuple);
flag = true;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (file != null) {
try {
file.close();
file = null;
} catch (Exception e) {
System.out.println("File closing operation failed");
e.printStackTrace();
}
}
}
}
}
I searched for answers in the web but did not find anything relevant.
Following code of snippet might help.
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
//For each row, iterate through all the columns
Iterator<Cell> cellIterator = row.cellIterator();
outer:
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
//will iterate over the Merged cells
for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
CellRangeAddress region = sheet.getMergedRegion(i); //Region of merged cells
int colIndex = region.getFirstColumn(); //number of columns merged
int rowNum = region.getFirstRow(); //number of rows merged
//check first cell of the region
if (rowNum == cell.getRowIndex() && colIndex == cell.getColumnIndex()) {
System.out.println(sheet.getRow(rowNum).getCell(colIndex).getStringCellValue());
continue outer;
}
}
//the data in merge cells is always present on the first cell. All other cells(in merged region) are considered blank
if (cell.getCellType() == Cell.CELL_TYPE_BLANK || cell == null) {
continue;
}
System.out.println(cell.getStringCellValue());
}
}
This method can read a specific cell (including merged cell):
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public static void readCell(String excelFilePath, int rowIndex, int columnIndex) throws FileNotFoundException, IOException {
try (InputStream inp = new FileInputStream(excelFilePath)) {
XSSFWorkbook wb = new XSSFWorkbook(inp);
XSSFCell cell = wb.getSheetAt(0).getRow(rowIndex).getCell(columnIndex);
switch (cell.getCellType()) {
case STRING:
System.out.println(cell.getRichStringCellValue().getString());
break;
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
System.out.println(cell.getDateCellValue());
} else {
System.out.println(cell.getNumericCellValue());
}
break;
case BOOLEAN:
System.out.println(cell.getBooleanCellValue());
break;
case FORMULA:
System.out.println(cell.getCellFormula());
break;
case BLANK:
System.out.println();
break;
default:
System.out.println();
}
wb.close();
}
}
Dependencies: POI 5.0.0, JDK 1.8.0

How to access rows and columns of excel file in java

Importing and exporting of excel file
import java.io.File;
import java.io.FileInputStream; import
java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; import
org.apache.poi.xssf.usermodel.XSSFCell; import
org.apache.poi.xssf.usermodel.XSSFRow; import
org.apache.poi.xssf.usermodel.XSSFSheet; import
org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelRead {
public static void first() throws FileNotFoundException, IOException {
// import of workbok//
File fs = new File("D:\\Input.xlsx");
FileInputStream fis = new FileInputStream(fs);
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheetAt(0);
int rownum = sheet.getLastRowNum() + 1;
int colnum = sheet.getRow(0).getLastCellNum();
String[][] data = new String[rownum][colnum];
for (int i = 0; i < rownum; i++) {
XSSFRow row = sheet.getRow(i);
for (int j = 0; j < colnum; j++) {
XSSFCell Cell = row.getCell(j);
data[i][j] = Cell.toString();
System.out.print(data[i][j] + "\t\t");
}
System.out.println("\t");
System.out.println("\t\t");
}
FileOutputStream fileOut = new FileOutputStream("D:/NewExcelFile.xlsx");
workbook.write(fileOut);
fileOut.close();
System.out.println("Your excel file has been generated!");
}
public static String CellToString() {
XSSFCell cell = null;
int type;
Object result;
type = cell.getCellType();
switch (type) {
case 0:
result = cell.getNumericCellValue();
break;
case 1:
result = cell.getStringCellValue();
break;
default:
throw new RuntimeException("There are no support for this type of cell");
}
return result.toString();
}
}
Try the below code:
FileInputStream file = new FileInputStream(new File("path to xlsx file"));
XSSFWorkbook wb = new XSSFWorkbook(file);
XSSFSheet sheet = wb.getSheetAt(0);
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext())
{
Row row = rowIterator.next();
Iterator <Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext())
{
Cell cell = cellIterator.next();
System.out.print(cell.getStringCellValue() + "\t\t");
}
}

Once I read a excel file with apache POI how do I print the sheet in a txt file?

First I would like to thank all for your time in reading my post!
I use netbeans IDE 8.0.1 and I´m new in JAVA.
I have been searching in google many ways to read & write excel files using Apache POI. There are a lot of tutorials and information about it, but I still can´t write the excel file (or sheet) that I read in a .txt file.
I have this code that is really good because declare a workbooks if is a xls or a xlsx file. But it dosen't print what is read as a table in netbeans, so I think that I will have the same problem if I try to print it as a .txt file.
Can someone help me please? Thanks and sorry for the code paste is my first post!
Here is the code:
package read_write_excel_V2;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class JIA_ReadingRewritingWorbooks_v2 {
public static void main(String[] args) throws IOException {
String fname = "ExcelJAVAWrite_demo.xlsx";
InputStream inp = new FileInputStream(fname);
String fileExtn = GetFileExtension(fname);
Workbook wb_xssf; // Declare XSSF WorkBook
Workbook wb_hssf; // Declare HSSF WorkBook
Sheet sheet = null;
if (fileExtn.equalsIgnoreCase("xlsx")) {
wb_xssf = new XSSFWorkbook(inp);
log("xlsx="+wb_xssf.getSheetName(0));
sheet = wb_xssf.getSheetAt(0);
}
if (fileExtn.equalsIgnoreCase("xls")) {
POIFSFileSystem fs = new POIFSFileSystem(inp);
wb_hssf = new HSSFWorkbook(fs);
log("xls="+wb_hssf.getSheetName(0));
sheet = wb_hssf.getSheetAt(0);
}
Iterator rows = sheet.rowIterator();
while (rows.hasNext()) {
Row row = (Row) rows.next();
Iterator cells = row.cellIterator();
while (cells.hasNext()){
Cell cell = (Cell) cells.next();
switch ( cell.getCellType() ) {
case Cell.CELL_TYPE_STRING:
log(cell.getRichStringCellValue().getString());
break;
case Cell.CELL_TYPE_NUMERIC:
if(DateUtil.isCellDateFormatted(cell)) {
log(cell.getDateCellValue()+"");
} else {
System.out.println(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_BOOLEAN:
log(cell.getBooleanCellValue()+"");
break;
case Cell.CELL_TYPE_FORMULA:
log(cell.getCellFormula());
break;
default:
}
}
}
inp.close();
}
private static void log(String message) {
System.out.println(message);
}
private static String GetFileExtension(String fname2) {
String fileName = fname2;
String fname="";
String ext="";
int mid= fileName.lastIndexOf(".");
fname=fileName.substring(0,mid);
ext=fileName.substring(mid+1,fileName.length());
return ext;
}
}
I add what #dkatzel recommends but the printing is still in rows. The code:
public class JIA_ReadingRewritingWorbooks_v2_Tmp {
public static void main(String[] args) throws IOException {
....
Iterator rows = sheet.rowIterator();
while (rows.hasNext()) {
Row row = (Row) rows.next();
int numCells =row.getLastCellNum();
for(int i=0; i<numCells; i++){
Cell cell = row.getCell(i);
// If not defined, cell could be null
// Do your cell printing here
//Cell cell = (Cell) cells.next();
switch ( cell.getCellType() ) {
case Cell.CELL_TYPE_STRING:
log(cell.getRichStringCellValue().getString());
break;
case Cell.CELL_TYPE_NUMERIC:
if(DateUtil.isCellDateFormatted(cell)) {
log(cell.getDateCellValue()+"");
} else {
System.out.println(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_BOOLEAN:
log(cell.getBooleanCellValue()+"");
break;
case Cell.CELL_TYPE_FORMULA:
log(cell.getCellFormula());
break;
default:
}
//}
}
}
inp.close();
}
private static void log(String message) {
System.out.println(message);
}
private static String GetFileExtension(String fname2) {
String fileName = fname2;
String fname="";
String ext="";
int mid= fileName.lastIndexOf(".");
fname=fileName.substring(0,mid);
ext=fileName.substring(mid+1,fileName.length());
return ext;
}
}
I have this results:
This type of results I´m getting:
https://fbcdn-sphotos-a-a.akamaihd.net/hphotos-ak-xpa1/v/t1.0-9/10527311_10153011702894311_4509818841257258400_n.jpg?oh=d724cbfc9a96aa29a07f0d26a7c81726&oe=54B5C2A5&gda=1421766835_79c0648ddbeb2eb966027cd62f8d241a
I wish to have this type of results, tabulated:
https://fbcdn-sphotos-b-a.akamaihd.net/hphotos-ak-xap1/v/t1.0-9/10710855_10153011703104311_5065636883843539807_n.jpg?oh=9bc931559b3e2380068b7a7ecac35b57&oe=54ABCEC3&gda=1421306774_8cf6356575f6c255c52f316ed9d1fba0
or print the results in .txt file. Thanks!
I think your problem is you use the cell iterator, which the Javadoc says
Cell iterator over the physically defined cells
Which I take to mean it will skip blank cells.
If you want to include blanks you will need to iterate over them manually:
instead of
Iterator cells = row.cellIterator();
while (cells.hasNext()){
Cell cell = (Cell) cells.next();
...
}
Do this:
int numCells =row.getLastCellNum();
for(int i=0; i<numCells; i++){
Cell cell = row.getCell(i);
//if not defined, cell could be null
//do your cell printing here
}
String actualOutputWBString = new XSSFExcelExtractor(actualWorkbook).getText();

java excel to csv file convert

I am trying to convert .xlsx file to .csv, convertion is happening but the data is not formatted properly. Please find code below and suggest changes to the code.
Here I am trying to read an .xlsx file and write it to a csv file i.e. converting xlsx to csv but I am not getting the .csv file in proper format all the data is displayed in a single but it must displayed like rows in Excel.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
class XlstoCSV
{
static void xls(File inputFile, File outputFile)
{
// For storing data into CSV files
StringBuffer data = new StringBuffer();
try
{
FileOutputStream fos = new FileOutputStream(outputFile);
// Get the workbook object for XLS file
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(inputFile));
// Get first sheet from the workbook
HSSFSheet sheet = workbook.getSheetAt(0);
Cell cell;
Row row;
// Iterate through each rows from first sheet
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext())
{
row = rowIterator.next();
// For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext())
{
cell = cellIterator.next();
switch (cell.getCellType())
{
case Cell.CELL_TYPE_BOOLEAN:
data.append(cell.getBooleanCellValue() + ",");
break;
case Cell.CELL_TYPE_NUMERIC:
data.append(cell.getNumericCellValue() + ",");
break;
case Cell.CELL_TYPE_STRING:
data.append(cell.getStringCellValue() + ",");
break;
case Cell.CELL_TYPE_BLANK:
data.append("" + ",");
break;
default:
data.append(cell + ",");
}
data.append('\n');
}
}
fos.write(data.toString().getBytes());
fos.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
File in`enter code here`putFile = new File("C:\test.xls");
File outputFile = new File("C:\output.csv");
xls(inputFile, outputFile);
}
}
Try Changing your code to this
your new line char has to be appended after the one row read
while (rowIterator.hasNext())
{
row = rowIterator.next();
// For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext())
{
cell = cellIterator.next();
switch (cell.getCellType())
{
case Cell.CELL_TYPE_BOOLEAN:
data.append(cell.getBooleanCellValue() + ",");
break;
case Cell.CELL_TYPE_NUMERIC:
data.append(cell.getNumericCellValue() + ",");
break;
case Cell.CELL_TYPE_STRING:
data.append(cell.getStringCellValue() + ",");
break;
case Cell.CELL_TYPE_BLANK:
data.append("" + ",");
break;
default:
data.append(cell + ",");
}
}
data.append('\n');
}
The problem is you are appending a new line after every cell, here:
data.append('\n');
You need to do it after every row instead.
Note also that you would be best using a proper CSV library for serialisation as it will handle escaping strings correctly.
Quite a old post but will help someone.. With Multiple Sheets.
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Iterator;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
class XlStoCSV {
static void xls(File inputFile) {
// Get the workbook object for XLS file
int count = 0;
try {
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(
inputFile));
for (int l = workbook.getNumberOfSheets() - 1; l >= 0; l--) {
File outputFile = new File(System.getProperty("user.dir")
+ "/output/"+inputFile.getName()+"-"+workbook.getSheetName(count) +".csv");
// For storing data into CSV files
StringBuffer data = new StringBuffer();
FileOutputStream fos = new FileOutputStream(outputFile);
HSSFSheet sheet = workbook.getSheetAt(count);
Cell cell;
Row row;
// Iterate through each rows from first sheet
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
row = rowIterator.next();
// For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
int columnNumber = 1;
while (cellIterator.hasNext()) {
cell = cellIterator.next();
if (columnNumber > 1)
{
data.append(",");
}
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
data.append(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_NUMERIC:
data.append(cell.getNumericCellValue() );
break;
case Cell.CELL_TYPE_STRING:
data.append(cell.getStringCellValue());
break;
case Cell.CELL_TYPE_BLANK:
data.append("");
break;
default:
data.append(cell);
}
++columnNumber;
}
data.append('\n');
}
fos.write(data.toString().getBytes());
fos.close();
count++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
File inputFile = new File(System.getProperty("user.dir") + "/assets/"
+ "test.xls");
xls(inputFile);
}
}
Try this :
public String convertRowContentToCSV(Row row) {
Iterator<Cell> cellIterator = row.cellIterator();
StringBuilder data = new StringBuilder();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch (cell.getCellTypeEnum()) {
case BOOLEAN:
data.append(cell.getBooleanCellValue()).append(",");
break;
case NUMERIC:
data.append(cell.getNumericCellValue()).append(",");
break;
case STRING:
data.append(cell.getStringCellValue()).append(",");
break;
case BLANK:
data.append(",");
break;
case FORMULA:
case _NONE:
case ERROR:
break;
default:
data.append(cell).append(",");
}
}
return data.toString();
}
Try this code:
import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.TimeoutException;
import java.util.logging.Logger;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbookFactory;
public class App {
public void convertExcelToCSV(Sheet sheet, String sheetName) {
StringBuffer data = new StringBuffer();
try {
FileOutputStream fos = new FileOutputStream("C:\\Users\\" + sheetName + ".csv");
Cell cell;
Row row;
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
cell = cellIterator.next();
CellType type = cell.getCellTypeEnum();
if (type == CellType.BOOLEAN) {
data.append(cell.getBooleanCellValue() + ",");
} else if (type == CellType.NUMERIC) {
data.append(cell.getNumericCellValue() + ",");
} else if (type == CellType.STRING) {
data.append(cell.getStringCellValue() + ",");
} else if (type == CellType.BLANK) {
data.append("" + ",");
} else {
data.append(cell + ",");
}
}
data.append('\n');
}
fos.write(data.toString().getBytes());
fos.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public static void main(String [] args)
{
App app = new App();
String path = "C:\\Users\\myFile.xlsx";
InputStream inp = null;
try {
inp = new FileInputStream(path);
Workbook wb = WorkbookFactory.create(inp);
for(int i=0;i<wb.getNumberOfSheets();i++) {
System.out.println(wb.getSheetAt(i).getSheetName());
app.convertExcelToCSV(wb.getSheetAt(i),wb.getSheetAt(i).getSheetName());
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
finally {
try {
inp.close();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
}

Convert .xls to .csv in Java

I want to convert the xls file to csv. I successfully converted it to a csv file, but the last column also has a comma appended. How do I remove the last comma, example 1,2,2,3,... Could you please help out?
package bwtest;
import java.io.*;
import java.util.Iterator;
import org.apache.poi.hssf.usermodel.HSSFSheet;
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.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
class ExcelToCSV {
static void convertToXlsx(File inputFile, File outputFile) {
// For storing data into CSV files
StringBuffer cellValue = new StringBuffer();
try {
FileOutputStream fos = new FileOutputStream(outputFile);
// Get the workbook instance for XLSX file
XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(inputFile));
// Get first sheet from the workbook
XSSFSheet sheet = wb.getSheetAt(0);
Row row;
Cell cell;
// Iterate through each rows from first sheet
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
row = rowIterator.next();
// For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
cellValue.append(cell.getBooleanCellValue() + ",");
break;
case Cell.CELL_TYPE_NUMERIC:
cellValue.append(cell.getNumericCellValue()
+ ",");
break;
case Cell.CELL_TYPE_STRING:
cellValue.append(cell.getStringCellValue() + ",");
break;
case Cell.CELL_TYPE_BLANK:
cellValue.append("" + ",");
break;
default:
cellValue.append(cell + ",");
}
}
}
fos.write(cellValue.toString().getBytes());
fos.close();
} catch (Exception e) {
System.err.println("Exception :" + e.getMessage());
}
}
static void convertToXls(File inputFile, File outputFile) {
// For storing data into CSV files
StringBuffer cellDData = new StringBuffer();
try {
FileOutputStream fos = new FileOutputStream(outputFile);
// Get the workbook instance for XLS file
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(
inputFile));
// Get first sheet from the workbook
HSSFSheet sheet = workbook.getSheetAt(0);
Cell cell;
Row row;
// Iterate through each rows from first sheet
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
row = rowIterator.next();
// For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
cellDData.append(cell.getBooleanCellValue() + ",");
break;
case Cell.CELL_TYPE_NUMERIC:
cellDData.append(cell.getNumericCellValue() + ",");
break;
case Cell.CELL_TYPE_STRING:
cellDData.append(cell.getStringCellValue() + ",");
break;
case Cell.CELL_TYPE_BLANK:
cellDData.append("" + ",");
break;
default:
cellDData.append(cell + ",");
}
}
}
fos.write(cellDData.toString().getBytes());
fos.close();
} catch (FileNotFoundException e) {
System.err.println("Exception" + e.getMessage());
} catch (IOException e) {
System.err.println("Exception" + e.getMessage());
}
}
public static void main(String[] args)
{
File inputFile = new File("C:\input.xls");
File outputFile = new File("C:\output1.csv");
File inputFile2 = new File("C:\input.xlsx");
File outputFile2 = new File("C:\output2.csv");
convertToXls(inputFile, outputFile);
convertToXlsx(inputFile2, outputFile2);
}
}
Assuming every row has cells:
After your cellIterator loop and before you rowIterator loop finishes, add:
cellDData.deleteCharAt(cellDData.length()-1);
This should delete the last comma in the line.
If it's possible to have a row where the cellIterator doesn't run(which I doubt) then you can put boolean hasCells = false; before the cellIterator loop, and set hasCells = true; inside of the loop somewhere. Then, only delete the comma if(hasCells)
Your conversion algorithm is incorrect.
Instead of adding value then comma,
you should add comma (if needed) then value.
Here is some code:
...
int columnNumber = 1;
while (cellIterator.hasNext())
{
if (columnNumber > 1)
{
cellValue.append(",")
}
row = rowIterator.next();
switch (cell.getCellType())
{
... append the cell value to the cellValue.
}
++columnNumber;
}
...
After each row you will need to insert a line feed "/r/n" and remove the last coma.
You can write it to the fos at that time.
if (cellDData != null && cellDData.length() > 1) {
String cellDDataString = cellDData.toString();
cellDDataString = cellDDataString.substring(0,cellDDataString.length() - 1) + "/r/n";
fos.write(cellDDataString);
}
cellDData = new StringBuffer();

Categories

Resources