Java POIを使用してExcelシートから値を読み取っていて、データベースに挿入する必要があります.Excelシートから文字列、数値、および日付の値があり、最初のフィールドは列ヘッダーです。
public class SimpleExcelReadExample {
static Connection con1 = null;
static Connection con3 = null;
static PreparedStatement preparedstatement = null;
static ResultSet resultset = null;
int j = 0;
public static void main(String[] args) {
String fileName = "D:/Excel/Report.xls";
Cleartables.table_daily_report();
Vector dataHolder = read(fileName);
saveToDatabase(dataHolder);
}
public static Vector read(String fileName) {
Vector cellVectorHolder = new Vector();
try {
FileInputStream myInput = new FileInputStream(fileName);
POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);
HSSFSheet mySheet = myWorkBook.getSheetAt(0);
Iterator rowIter = mySheet.rowIterator();
while (rowIter.hasNext()) {
HSSFRow myRow = (HSSFRow) rowIter.next();
Iterator cellIter = myRow.cellIterator();
Vector cellStoreVector = new Vector();
while (cellIter.hasNext()) {
HSSFCell myCell = (HSSFCell) cellIter.next();
//System.out.println("read method"+myCell);
cellStoreVector.addElement(myCell);
}
cellVectorHolder.addElement(cellStoreVector);
}
} catch (Exception e) {
e.printStackTrace();
}
return cellVectorHolder;
}
private static void saveToDatabase(Vector dataHolder)
{
for (int i=0;i<dataHolder.size(); i++)
{
Vector cellStoreVector=(Vector)dataHolder.elementAt(i);
for (int j=0; j < cellStoreVector.size();j++)
{
System.out.println("show.....");
HSSFCell myCell = (HSSFCell)cellStoreVector.elementAt(j);
Please help me here....How to get the each column values ?
}