如何将 XLSX 文件转换为字体格式为 java 的 CSV
How to convert XLSX file to CSV with font formats in java
我正在尝试将我的 xls 文件转换为 csv 文件。我的 java 代码正确转换文本。但我没有得到任何字体格式和其他格式。
那么如何在我的 CSV(在 excel 中打开)文件中获取我的字体格式。
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
}
}
CSV 代表逗号分隔值。真的就是这样。它是文本,值由逗号分隔(或者,在某些版本中,分号或其他分隔符)。它基本上是一个纯文本文件,可以用文本编辑器打开和编辑。无法在该文件格式中包含格式信息。
我正在尝试将我的 xls 文件转换为 csv 文件。我的 java 代码正确转换文本。但我没有得到任何字体格式和其他格式。 那么如何在我的 CSV(在 excel 中打开)文件中获取我的字体格式。
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
}
}
CSV 代表逗号分隔值。真的就是这样。它是文本,值由逗号分隔(或者,在某些版本中,分号或其他分隔符)。它基本上是一个纯文本文件,可以用文本编辑器打开和编辑。无法在该文件格式中包含格式信息。