xlsx2csv java 哪个包_java poi xlsx2csv

import org.apache.poi.openxml4j.opc.OPCPackage;

import org.apache.poi.openxml4j.opc.PackageAccess;

import org.apache.poi.ss.usermodel.DataFormatter;

import org.apache.poi.ss.util.CellAddress;

import org.apache.poi.ss.util.CellReference;

import org.apache.poi.util.SAXHelper;

import org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable;

import org.apache.poi.xssf.eventusermodel.XSSFReader;

import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler;

import org.apache.poi.xssf.model.StylesTable;

import org.apache.poi.xssf.usermodel.XSSFComment;

import org.xml.sax.ContentHandler;

import org.xml.sax.InputSource;

import org.xml.sax.SAXException;

import org.xml.sax.XMLReader;

import javax.xml.parsers.ParserConfigurationException;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import static com.cindata.XSSFSheetXMLHandler.SheetContentsHandler;

import grails.transaction.Transactional

@Transactional

class ExcelXlsx2CsvService {

/**

* Uses the XSSF Event SAX helpers to do most of the work

* of parsing the Sheet XML, and outputs the contents

* as a (basic) CSV.

*/

private static class SheetToCSV implements SheetContentsHandler {

private boolean firstCellOfRow = false;

private int currentRow = -1;

private int currentCol = -1;

private StringBuffer lineBuffer = new StringBuffer();

/**

* Destination for data

*/

private FileOutputStream outputStream;

public SheetToCSV(FileOutputStream outputStream) {

this.outputStream = outputStream;

}

@Override

public void startRow(int rowNum) {

/**

* If there were gaps, output the missing rows:

* outputMissingRows(rowNum - currentRow - 1);

*/

// Prepare for this row

firstCellOfRow = true;

currentRow = rowNum;

currentCol = -1;

lineBuffer.delete(0, lineBuffer.length()); //clear lineBuffer

}

@Override

public void endRow(int rowNum) {

lineBuffer.append('\n');

try {

outputStream.write(lineBuffer.substring(0).getBytes());

} catch (IOException e) {

// log.error("save date to file error at row number: {}", currentCol);

throw new RuntimeException("save date to file error at row number: " + currentCol);

}

}

@Override

public void cell(String cellReference, String formattedValue, XSSFComment comment) {

if (firstCellOfRow) {

firstCellOfRow = false;

} else {

lineBuffer.append(',');

}

// gracefully handle missing CellRef here in a similar way as XSSFCell does

if (cellReference == null) {

cellReference = new CellAddress(currentRow, currentCol).formatAsString();

}

int thisCol = (new CellReference(cellReference)).getCol();

//空缺单元格的个数,合并单元格和没有内容的单元格都算是丢失的col

int missedCols = thisCol - currentCol - 1;

if (missedCols >= 1) { //合并单元格的地方,不打印逗号

for(int i=0;i

lineBuffer.append(',');

}

}

currentCol = thisCol;

if (formattedValue.contains("\n")) { //去除换行符

formattedValue = formattedValue.replace("\n", "");

}

// formattedValue = "\"" + formattedValue + "\""; //有些excel文档 2300的数值为2,300

formattedValue = formattedValue.replace(',',',')

lineBuffer.append(formattedValue);

}

@Override

public void headerFooter(String text, boolean isHeader, String tagName) {

// Skip, no headers or footers in CSV

}

}

/**

* Parses and shows the content of one sheet

* using the specified styles and shared-strings tables.

*

* @param styles The table of styles that may be referenced by cells in the sheet

* @param strings The table of strings that may be referenced by cells in the sheet

* @param sheetInputStream The stream to read the sheet-data from.

* @throws java.io.IOException An IO exception from the parser,

* possibly from a byte stream or character stream

* supplied by the application.

* @throws SAXException if parsing the XML data fails.

*/

private static void processSheet(StylesTable styles, ReadOnlySharedStringsTable strings,

com.cindata.XSSFSheetXMLHandler.SheetContentsHandler sheetHandler, InputStream sheetInputStream) throws Exception {

DataFormatter formatter = new DataFormatter();

InputSource sheetSource = new InputSource(sheetInputStream);

try {

XMLReader sheetParser = SAXHelper.newXMLReader();

ContentHandler handler = new com.cindata.XSSFSheetXMLHandler(

styles, null, strings, sheetHandler, formatter, false);

sheetParser.setContentHandler(handler);

sheetParser.parse(sheetSource);

} catch (ParserConfigurationException e) {

throw new RuntimeException("SAX parser appears to be broken - " + e.getMessage());

}

}

/**

* Initiates the processing of the XLS workbook file to CSV.

*

* @throws IOException If reading the data from the package fails.

* @throws SAXException if parsing the XML data fails.

*/

public static String process(String srcFile) throws Exception {

int index0 = srcFile.lastIndexOf('.')

String destFile = srcFile.substring(0,index0)+".csv"

File xlsxFile = new File(srcFile);

OPCPackage xlsxPackage = OPCPackage.open(xlsxFile.getPath(), PackageAccess.READ);

ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(xlsxPackage);

XSSFReader xssfReader = new XSSFReader(xlsxPackage);

StylesTable styles = xssfReader.getStylesTable();

XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) xssfReader.getSheetsData();

int index = 0;

// while (iter.hasNext()) {

if(iter){

InputStream stream = iter.next();

String sheetName = iter.getSheetName();

// log.info(sheetName + " [index=" + index + "]");

FileOutputStream fileOutputStream = new FileOutputStream(destFile);

processSheet(styles, strings, new SheetToCSV(fileOutputStream), stream);

stream.close();

fileOutputStream.flush();

fileOutputStream.close();

++index;

}

// }

xlsxPackage.close();

return destFile

}

}

以下是Java代码示例,用于将Excel文件(.xlsx格式)转换为CSV文件: ```java import java.io.*; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.*; public class XlsxToCsvConverter { public static void main(String[] args) { String xlsxFilePath = "input.xlsx"; // 输入的Excel文件路径 String csvFilePath = "output.csv"; // 输出的CSV文件路径 convertXlsxToCsv(xlsxFilePath, csvFilePath); } public static void convertXlsxToCsv(String xlsxFilePath, String csvFilePath) { try { // 读取Excel文件 FileInputStream inputFile = new FileInputStream(new File(xlsxFilePath)); Workbook workbook = new XSSFWorkbook(inputFile); Sheet sheet = workbook.getSheetAt(0); // 创建CSV文件并写入数据 FileWriter outputFile = new FileWriter(new File(csvFilePath)); for (Row row : sheet) { for (Cell cell : row) { switch (cell.getCellType()) { case STRING: outputFile.write(cell.getStringCellValue()); break; case NUMERIC: outputFile.write(Double.toString(cell.getNumericCellValue())); break; case BOOLEAN: outputFile.write(Boolean.toString(cell.getBooleanCellValue())); break; default: outputFile.write(""); } outputFile.write(","); } outputFile.write("\n"); } // 关闭文件流 inputFile.close(); outputFile.flush(); outputFile.close(); System.out.println("Excel文件已成功转换为CSV文件!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 这里使用了Apache POI库来读取Excel文件,需要在项目中添加以下依赖项: ```xml <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>5.0.0</version> </dependency> ``` 请注意,此代码示例仅适用于Excel文件中只含一个工作表的情况。如果有多个工作表,请根据需要进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值