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.
我试图将.xlsx文件转换为.csv,转换正在发生,但数据格式不正确。请在下面找到代码,并建议更改代码。
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.
在这里,我试图读取一个.xlsx文件并将其写入csv文件,即将xlsx转换为csv但是我没有以正确的格式获取.csv文件所有数据都显示在一个单独但它必须在Excel中显示为行。
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;
public class XlsxtoCSV {
static void xlsx(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 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();
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 + ",");
}
}
}
fos.write(data.toString().getBytes());
fos.close();
} catch (Exception ioe) {
ioe.printStackTrace();
}
}
//testing the application
public static void main(String[] args) {
//reading file from desktop
File inputFile = new File("C:\\Users\\user69\\Desktop\\test.xlsx");
//writing excel data to csv
File outputFile = new File("C:\\Users\\user69\\Desktop\\test1.csv");
xlsx(inputFile, outputFile);
}
}
2 个解决方案
#1
11
This is thanks @Swapnil!
这是谢谢@Swapnil!
data.append("\r\n"); // After the columns have been appended.
#2
0
public static void convertToXlsx(File inputFile, File outputFile) {
StringBuffer bf = new StringBuffer();
FileOutputStream fos = null;
String strGetValue = "";
try {
fos = new FileOutputStream(outputFile);
XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(inputFile));
XSSFSheet sheet = wb.getSheetAt(0);
Row row;
Cell cell;
int intRowCounter = 0;
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
StringBuffer cellDData = new StringBuffer();
row = rowIterator.next();
int maxNumOfCells = sheet.getRow(0).getLastCellNum();
int cellCounter = 0;
while ((cellCounter) < maxNumOfCells) {
if (sheet.getRow(row.getRowNum()) != null
&& sheet.getRow(row.getRowNum()).getCell(cellCounter) != null) {
cell = sheet.getRow(row.getRowNum()).getCell(cellCounter);
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
strGetValue = cell.getBooleanCellValue() + ",";
cellDData.append(removeSpace(strGetValue));
break;
case Cell.CELL_TYPE_NUMERIC:
strGetValue = new BigDecimal(cell.getNumericCellValue()).toPlainString();
if (DateUtil.isCellDateFormatted(cell)) {
strGetValue = new DataFormatter().formatCellValue(cell);
} else {
strGetValue = new BigDecimal(cell.getNumericCellValue()).toPlainString();
}
String tempStrGetValue = removeSpace(strGetValue);
if (tempStrGetValue.length() == 0) {
strGetValue = " ,";
cellDData.append(strGetValue);
} else {
strGetValue = strGetValue + ",";
cellDData.append(removeSpace(strGetValue));
}
break;
case Cell.CELL_TYPE_STRING:
strGetValue = cell.getStringCellValue();
String tempStrGetValue1 = removeSpace(strGetValue);
if (tempStrGetValue1.length() == 0) {
strGetValue = " ,";
cellDData.append(strGetValue);
} else {
strGetValue = strGetValue + ",";
cellDData.append(removeSpace(strGetValue));
}
break;
case Cell.CELL_TYPE_BLANK:
strGetValue = "" + ",";
cellDData.append(removeSpace(strGetValue));
break;
default:
strGetValue = cell + ",";
cellDData.append(removeSpace(strGetValue));
}
} else {
strGetValue = " ,";
cellDData.append(strGetValue);
}
cellCounter++;
}
String temp = cellDData.toString();
if (temp != null && temp.contains(",,,")) {
temp = temp.replaceFirst(",,,", ", ,");
}
if (temp.endsWith(",")) {
temp = temp.substring(0, temp.lastIndexOf(","));
cellDData = null;
bf.append(temp.trim());
}
bf.append("\n");
intRowCounter++;
}
fos.write(bf.toString().getBytes());
fos.close();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (fos != null)
fos.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
private static String removeSpace(String strString) {
if (strString != null && !strString.equals("")) {
return strString.trim();
}
return strString;
}
- code example handle , and space character in cell
#
- 代码示例句柄和单元格中的空格字符#
#1
11
This is thanks @Swapnil!
这是谢谢@Swapnil!
data.append("\r\n"); // After the columns have been appended.
#2
0
public static void convertToXlsx(File inputFile, File outputFile) {
StringBuffer bf = new StringBuffer();
FileOutputStream fos = null;
String strGetValue = "";
try {
fos = new FileOutputStream(outputFile);
XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(inputFile));
XSSFSheet sheet = wb.getSheetAt(0);
Row row;
Cell cell;
int intRowCounter = 0;
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
StringBuffer cellDData = new StringBuffer();
row = rowIterator.next();
int maxNumOfCells = sheet.getRow(0).getLastCellNum();
int cellCounter = 0;
while ((cellCounter) < maxNumOfCells) {
if (sheet.getRow(row.getRowNum()) != null
&& sheet.getRow(row.getRowNum()).getCell(cellCounter) != null) {
cell = sheet.getRow(row.getRowNum()).getCell(cellCounter);
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
strGetValue = cell.getBooleanCellValue() + ",";
cellDData.append(removeSpace(strGetValue));
break;
case Cell.CELL_TYPE_NUMERIC:
strGetValue = new BigDecimal(cell.getNumericCellValue()).toPlainString();
if (DateUtil.isCellDateFormatted(cell)) {
strGetValue = new DataFormatter().formatCellValue(cell);
} else {
strGetValue = new BigDecimal(cell.getNumericCellValue()).toPlainString();
}
String tempStrGetValue = removeSpace(strGetValue);
if (tempStrGetValue.length() == 0) {
strGetValue = " ,";
cellDData.append(strGetValue);
} else {
strGetValue = strGetValue + ",";
cellDData.append(removeSpace(strGetValue));
}
break;
case Cell.CELL_TYPE_STRING:
strGetValue = cell.getStringCellValue();
String tempStrGetValue1 = removeSpace(strGetValue);
if (tempStrGetValue1.length() == 0) {
strGetValue = " ,";
cellDData.append(strGetValue);
} else {
strGetValue = strGetValue + ",";
cellDData.append(removeSpace(strGetValue));
}
break;
case Cell.CELL_TYPE_BLANK:
strGetValue = "" + ",";
cellDData.append(removeSpace(strGetValue));
break;
default:
strGetValue = cell + ",";
cellDData.append(removeSpace(strGetValue));
}
} else {
strGetValue = " ,";
cellDData.append(strGetValue);
}
cellCounter++;
}
String temp = cellDData.toString();
if (temp != null && temp.contains(",,,")) {
temp = temp.replaceFirst(",,,", ", ,");
}
if (temp.endsWith(",")) {
temp = temp.substring(0, temp.lastIndexOf(","));
cellDData = null;
bf.append(temp.trim());
}
bf.append("\n");
intRowCounter++;
}
fos.write(bf.toString().getBytes());
fos.close();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (fos != null)
fos.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
private static String removeSpace(String strString) {
if (strString != null && !strString.equals("")) {
return strString.trim();
}
return strString;
}
- code example handle , and space character in cell
#
- 代码示例句柄和单元格中的空格字符#