而将Excel转换为.csv文件,我不能够转换date,并将其保存在CSV文件?
我正在使用Apache POI 3.6,我想从Excel中读取数据,并将Excel数据转换为.CVS文件。 我的Excel数据将以这种格式
- 第一列将是:date(8/6/2009)
- 第二栏将是:时间(上午12:00:01)
- 第三列将是:值1(30)
- 第四列将是:值2(400.99)。
我需要以这种格式(8-6-2009)转换这些数据,并将其存储在CVS文件中。错误:我可以将数据存储在csv文件中,但不能将其中的date和时间转换。 它在date列中获取41426一些值,在时间列我得到这个3456.0。
我没有得到如何转换这上面:请任何人帮我出.. 🙁
我的代码在这里:
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.DateUtil; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class Excel { static void xlsx(File inputFile, File outputFile) { // For storing data into CSV files StringBuffer data = new StringBuffer(); try { FileOutputStream fos = new FileOutputStream(outputFile); FileInputStream fio = new FileInputStream(inputFile); // Get the workbook object for XLSX file XSSFWorkbook wBook = new XSSFWorkbook(fio); // 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: if (row.getCell(0).getCellType() == Cell.CELL_TYPE_NUMERIC){ data.append(cell.getNumericCellValue() + ","); } if (DateUtil.isCellDateFormatted(row.getCell(0))) { System.out.println ("Row No.: " + row.getRowNum ()+ " " + row.getCell(0).getDateCellValue()); data.append(row.getCell(0).getDateCellValue() + ","); } 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("\r\n"); }//System.out.println(); //System.out.println(data); 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("D:\\project-work\\sampleData.xlsx"); //writing excel data to csv File outputFile = new File("D:\\project-work\\Data.csv"); xlsx(inputFile, outputFile); }
如果Excel的单元格设置正确的date/时间,你应该能够通过获取实际的date
Date date = cell.getDateCellValue()
然后,您可以使用SimpleDateFormatter
或类似的格式,以任何您需要在您的CSV格式。
- 根据一列读取excel数据并提交给java类
- 如何在java中使用selenium webdriver中的apache poi在excel文件中创build新工作表
- 使用java创buildExcel表格时遇到的问题
- Apache POI Excel行颜色只有黑色,不会更改
- Apache POI条件格式 – getNumConditionalFormattings总是返回0
- 通过我的Google应用程序引擎应用程序上传大量的excel文件进行查询
- 在AWS Lambda上parsingExcel文档
- string在spring自动转换
- 编写Excel文件时出现Apache POI OutOfMemoryError