Reputation: 2201
I am converting XLSX to CSV and uploading data into DB using below code,
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);
}
}
Above code uploading data to table but blank values not detecting by code and NULL values stores in table for blank values and date format always stores in 2033-01-02 instead of 1990-05-19.
Where am i doing wrong?
Upvotes: 0
Views: 88
Reputation: 22993
Excel doesn't know a Date
for a cell. The value is stored as a number and only the format make it a date.
If your cell contains the value 33012
and you specify a date format yyyy-mm-dd
it will be shown in Excel as 1990-05-19
. When you extract the data you will get the numeric value for this cell.
You need to differentiate the numeric values which should be interpreted as a date from normal numeric values. Find below a snippet as example.
import org.apache.poi.ss.usermodel.DateUtil;
...
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
// append as String in the format specified in Excel
// System.out.println("format: " + cell.getCellStyle().getDataFormatString());
data.append(cell);
} else {
data.append(cell.getNumericCellValue());
}
Upvotes: 2