空白未在XSSFWorksheet代码中检测到

时间:2015-08-25 04:59:49

标签: java excel apache-poi

我正在使用以下代码将XLSX转换为CSV并将数据上传到数据库

import java.io.*;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
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 XLStoCSVConvert {
    static Connectivity conv;
    static Connection con = null;
    static Statement st = null;
    static String query=null;
     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
                System.out.println("working......");
                XSSFWorkbook wBook = new XSSFWorkbook(new FileInputStream(inputFile));
                // Get first sheet from the workbook
                System.out.println("working......");
                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 + ",");

                        }

                    }
                    data.append("\n");
                }

                fos.write(data.toString().getBytes());
                fos.close();


                conv = new Connectivity();
                con = conv.setConnection();
                st = con.createStatement();
                query = "LOAD DATA LOCAL INFILE \"" + outputFile + "\" INTO TABLE xLSXUpload FIELDS TERMINATED BY ',' IGNORE 1 LINES";

                st.executeUpdate(query);



            } catch (Exception ioe) {
                ioe.printStackTrace();
            }
        }

    public static void main(String[] args) {
        try
        {
            //FileOutputStream fin = new FileOutputStream("/home/raptorjd4/Desktop/RaptorTrackingSystem/mani.csv");
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        File inputFile = new File("/home/raptorjd4/Desktop/XLS files/ToConsult.xlsx");
        //writing excel data to csv 
        File outputFile = new File("/home/raptorjd4/Desktop/RaptorTrackingSystem/ToConsult.csv");
        xlsx(inputFile, outputFile);

    }

}

以上代码将数据上传到表但空白值未通过代码检测,NULL值存储在表中以显示空白值,而日期格式始终存储在 2033-01-02而不是1990- 05-19

我在哪里做错了?

0 个答案:

没有答案