Java 将excel行数据写入sql server表时,每次都会插入相同的行

Java 将excel行数据写入sql server表时,每次都会插入相同的行,java,sql-server,excel,apache-poi,Java,Sql Server,Excel,Apache Poi,我有一个和excel表,其中有几行必须写入SQL server表。这是我的密码 我在插入之前执行一些转换,例如转换日期等。每次插入的都是同一行。如果我检查for循环中的输出行号,我会得到正确的结果,但每次都插入相同的行。不知道我哪里出错了。插入后,我想删除该行并将其写入另一个文件。但是,我无法将其写入另一个文件。文件大小正在增加,不会将任何行复制到其他文件 public void runit() throws Exception { boolean success; succes

我有一个和excel表,其中有几行必须写入SQL server表。这是我的密码

我在插入之前执行一些转换,例如转换日期等。每次插入的都是同一行。如果我检查for循环中的输出行号,我会得到正确的结果,但每次都插入相同的行。不知道我哪里出错了。插入后,我想删除该行并将其写入另一个文件。但是,我无法将其写入另一个文件。文件大小正在增加,不会将任何行复制到其他文件

public void runit() throws Exception
{
    boolean success;
    success = false;

    try
    {
        File folder = new File("c:\\DCIN_LOG\\dcp_dcl_mfg_order_status\\SQL DATA BASE DCP_2015\\SQL database  DCP.xls");

        if(!(folder.exists()))
            JOptionPane.showMessageDialog(null, "Input File Not Found");

        else
        {
            Statement stat = con.createStatement();
            FileInputStream fs = new FileInputStream(folder);
            HSSFWorkbook wb = new HSSFWorkbook(fs);
            HSSFSheet sheet = wb.getSheetAt(0);
            int rows = sheet.getPhysicalNumberOfRows();
            System.out.println("rows : " + rows);

            int coln = 0, coln1 = 0, coln2 = 0, coln3 = 0, coln4 = 0, coln5 = 0, coln6 = 0, coln7 = 0, coln8 = 0, coln9 = 0, coln10 = 0, coln11 = 0, coln12 = 0, coln13 = 0;

            HSSFRow rown = sheet.getRow(0);

            for(int c = 1; c < 15; c++)
            {
                HSSFCell celln = rown.getCell((short) c, rown.CREATE_NULL_AS_BLANK);

                if(("Sold-to pt").equals(celln.toString().trim()))
                {
                        coln = celln.getColumnIndex();
                }

                if(("Customer").equals(celln.toString().trim()))
                {
                        coln1 = celln.getColumnIndex();
                }

                if(("Purchase order no.").equals(celln.toString().trim()))
                {
                        coln2 = celln.getColumnIndex();
                }

                if(("PO Date").equals(celln.toString().trim()))
                {
                        coln3 = celln.getColumnIndex();
                }

                if(("SO No.").equals(celln.toString().trim()))
                {
                        coln4 = celln.getColumnIndex();
                }

                if(("SO Dtd.").equals(celln.toString().trim()))
                {
                        coln5 = celln.getColumnIndex();
                }

                if(("Line Item").equals(celln.toString().trim()))
                {
                        coln6 = celln.getColumnIndex();
                }

                if(("Part No.").equals(celln.toString().trim()))
                {
                        coln7 = celln.getColumnIndex();
                }

                if(("Description").equals(celln.toString().trim()))
                {
                        coln8 = celln.getColumnIndex();
                }

                if(("Order Qty. ").equals(celln.toString().trim()))
                {
                        coln9 = celln.getColumnIndex();
                }

                if(("Zone").equals(celln.toString().trim()))
                {
                        coln10 = celln.getColumnIndex();
                }

                if(("KAM").equals(celln.toString().trim()))
                {
                        coln11 = celln.getColumnIndex();
                }

                if(("Customer request dt:").equals(celln.toString().trim()))
                {
                        coln12 = celln.getColumnIndex();
                }

                if(("Delivery -Full/partial").equals(celln.toString().trim()))
                {
                        coln13 = celln.getColumnIndex();
                }
            }

            for(int r = 1; r < rows; r++)
            {
                HSSFRow row = sheet.getRow(r);
                System.out.println("r just in loop : " + r);

                if(row != null)
                {
                    int k = 0;

                    HSSFCell cell = row.getCell((short) 1);
                    HSSFCell cell1 = row.getCell((short) 2);
                    HSSFCell cell2 = row.getCell((short) 3);
                    HSSFCell cell3 = row.getCell((short) 4);
                    HSSFCell cell4 = row.getCell((short) 5);
                    HSSFCell cell5 = row.getCell((short) 6);
                    HSSFCell cell6 = row.getCell((short) 7);
                    HSSFCell cell7 = row.getCell((short) 8);
                    HSSFCell cell8 = row.getCell((short) 9);
                    HSSFCell cell9 = row.getCell((short) 10);
                    HSSFCell cell10 = row.getCell((short) 11);
                    HSSFCell cell11 = row.getCell((short) 12);
                    HSSFCell cell12 = row.getCell((short) 13);
                    HSSFCell cell13 = row.getCell((short) 14);

                    Statement st = con.createStatement();

                    String rt = "";

                    ResultSet rs = st.executeQuery("Select RoutingTime from DCP.dbo.Routing_Time where PartDesc = '"+cell8.toString().trim()+"'");

                    while(rs.next())
                    {
                            rt = rs.getString("RoutingTime");
                            System.out.println("RoutingTime : " + rt);
                    }

                    rs.close();

                    st.close();

                    String val = "", val1 = "", val2 = "", val3 = "";

                    val = cell3.toString().trim();
                    val1 = cell5.toString().trim();
                    val2 = cell12.toString().trim();

                    SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
                    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.S");

                    Calendar cal = Calendar.getInstance();
                    Calendar cal1 = Calendar.getInstance();
                    Calendar cal2 = Calendar.getInstance();
                    Calendar cal3 = Calendar.getInstance();
                    Calendar cal4 = Calendar.getInstance();

                    val3 = sdf.format(cal4.getTime());

                    cal4.setTime(sdf.parse(val3));

                    val3 = sdf1.format(cal4.getTime());
                    System.out.println("Current Date : " + val3);

                    cal1.setTime(sdf.parse(val));

                    val = sdf1.format(cal1.getTime());
                    System.out.println("val : " + val);

                    cal2.setTime(sdf.parse(val1));

                    val1 = sdf1.format(cal2.getTime());
                    System.out.println("val1 : " + val1);

                    cal3.setTime(sdf.parse(val2));

                    val2 = sdf1.format(cal3.getTime());
                    System.out.println("val2 : " + val2);

                    String together = cell13.toString().trim();

                    if(("full").equals(together.toLowerCase().trim()))
                            together = "Yes";

                    else
                            together = "No";

                    int index = cell4.toString().trim().indexOf(".");

                    String sonum = cell4.toString().trim().substring(0, index).trim();

                    int index1 = cell.toString().trim().indexOf(".");

                    String stp = cell.toString().trim().substring(0, index1).trim();

                    int index2 = cell6.toString().trim().indexOf(".");

                    String lnum = cell6.toString().trim().substring(0, index2).trim();

                    int index3 = cell9.toString().trim().indexOf(".");

                    String qty = cell9.toString().trim().substring(0, index3).trim();


                    k = stat.executeUpdate("INSERT INTO DCP.dbo.DCPDate(PO, PODate, SoldToParty, Cust, CreatedBy, SONo, SODate, SORDate, LineItemNo, PartNo, PartDesc, Qty, CustomerRequestDate, RoutingTime, SalesP, Zone, Together) VALUES ('"+cell2.toString().trim()+"', '"+val.trim()+"', '"+stp.trim()+"', '"+cell1.toString().trim()+"', '"+cell11.toString().trim()+"', '"+sonum.trim()+"', '"+val1.trim()+"', '"+val3.trim()+"', '"+lnum.trim()+"', '"+cell7.toString().trim()+"', '"+cell8.toString().trim()+"', '"+qty.trim()+"', '"+val2.trim()+"', '"+rt.trim()+"', '"+cell11.toString().trim()+"', '"+cell10.toString().trim()+"', '"+together.trim()+"')");

                    if(k > 0)
                    {
                        System.out.println("Data Inserted");

                        HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream("c:\\DCIN_LOG\\dcp_dcl_mfg_order_status\\SQL DATA BASE DCP_2015\\SQL database  DCP.xls"));

                        HSSFSheet sheet1 = workbook.getSheet("Sheet1");

                        File folder1 = new File("c:\\DCIN_LOG\\dcp_dcl_mfg_order_status\\SQL DATA BASE DCP_2015\\output.xls");

                        final String fileName1 = folder1.getAbsolutePath();

                        FileInputStream myInput1 = new FileInputStream(fileName1);

                        POIFSFileSystem myFileSystem1 = new POIFSFileSystem(myInput1);

                        HSSFWorkbook myWorkBook1 = new HSSFWorkbook(myFileSystem1);

                        HSSFSheet mySheet1 = myWorkBook1.getSheetAt(0);

                        int lastr = mySheet1.getLastRowNum();

                        copyRow(wb, sheet, r, lastr);

                        FileOutputStream out = new FileOutputStream("c:\\DCIN_LOG\\dcp_dcl_mfg_order_status\\SQL DATA BASE DCP_2015\\output.xls", true);

                        workbook.write(out);

                        out.close();
                    }

                    else
                        System.out.println("Data Not Inserted");

                    System.out.println("r after insertion: " + r);
                }

                System.out.println("r after one iteration: " + r);
            }

            success = true;
            stat.close();
        }
    }
    catch(Exception e)
    {
            e.printStackTrace();
            success = false;
    }
    System.out.println("Success : " + success);
}

public void copyRow(HSSFWorkbook workbook, HSSFSheet worksheet, int sourceRowNum, int destinationRowNum)
{
    // Get the source / new row
    HSSFRow newRow = worksheet.getRow(destinationRowNum);
    HSSFRow sourceRow = worksheet.getRow(sourceRowNum);

    // If the row exist in destination, push down all rows by 1 else create a new row
    if(newRow != null)
            worksheet.shiftRows(destinationRowNum, worksheet.getLastRowNum(), 1);

    else
            newRow = worksheet.createRow(destinationRowNum);

    // Loop through source columns to add to new row
    for(int i = 0; i < sourceRow.getLastCellNum(); i++)
    {
        // Grab a copy of the old / new cell
        HSSFCell oldCell = sourceRow.getCell(i);
        HSSFCell newCell = newRow.createCell(i);

        // If the old cell is null jump to next cell
        if(oldCell == null)
        {
            newCell = null;

            continue;
        }

        // Copy style from old cell and apply to new cell
        HSSFCellStyle newCellStyle = workbook.createCellStyle();

        newCellStyle.cloneStyleFrom(oldCell.getCellStyle());

        ;

        newCell.setCellStyle(newCellStyle);

        // If there is a cell comment, copy
        if(oldCell.getCellComment() != null)
            newCell.setCellComment(oldCell.getCellComment());

        // If there is a cell hyperlink, copy
        if(oldCell.getHyperlink() != null)
            newCell.setHyperlink(oldCell.getHyperlink());

        // Set the cell data type
        newCell.setCellType(oldCell.getCellType());

        // Set the cell data value
        switch(oldCell.getCellType())
        {
        case Cell.CELL_TYPE_BLANK: newCell.setCellValue(oldCell.getStringCellValue());
                                        break;

        case Cell.CELL_TYPE_BOOLEAN: newCell.setCellValue(oldCell.getBooleanCellValue());
                                        break;

        case Cell.CELL_TYPE_ERROR: newCell.setCellErrorValue(oldCell.getErrorCellValue());
                                        break;

        case Cell.CELL_TYPE_FORMULA: newCell.setCellFormula(oldCell.getCellFormula());
                                        break;

        case Cell.CELL_TYPE_NUMERIC: newCell.setCellValue(oldCell.getNumericCellValue());
                                        break;

        case Cell.CELL_TYPE_STRING: newCell.setCellValue(oldCell.getRichStringCellValue());
                                        break;
        }
    }

    // If there are are any merged regions in the source row, copy to new row
    for(int i = 0; i < worksheet.getNumMergedRegions(); i++)
    {
        CellRangeAddress cellRangeAddress = worksheet.getMergedRegion(i);

        if(cellRangeAddress.getFirstRow() == sourceRow.getRowNum())
        {
            CellRangeAddress newCellRangeAddress = new CellRangeAddress(newRow.getRowNum(), (newRow.getRowNum() + (cellRangeAddress.getLastRow() - cellRangeAddress.getFirstRow())), cellRangeAddress.getFirstColumn(), cellRangeAddress.getLastColumn());

            worksheet.addMergedRegion(newCellRangeAddress);
        }
    }
}
public void runit()引发异常
{
布尔成功;
成功=错误;
尝试
{
文件夹=新文件(“c:\\DCIN\u LOG\\dcp\u dcl\u mfg\u order\u status\\SQL数据库dcp\u 2015\\SQL数据库dcp.xls”);
如果(!(folder.exists()))
showMessageDialog(null,“未找到输入文件”);
其他的
{
Statement stat=con.createStatement();
FileInputStream fs=新的FileInputStream(文件夹);
HSSF工作手册wb=新的HSSF工作手册(fs);
HSSFSheet sheet=wb.getSheetAt(0);
int rows=sheet.getPhysicalNumberOfRows();
System.out.println(“行:”+行);
int coln=0,coln1=0,coln2=0,coln3=0,coln4=0,coln5=0,coln6=0,coln7=0,coln8=0,coln9=0,coln10=0,coln11=0,coln12=0,coln13=0;
HSSFRow rown=sheet.getRow(0);
对于(int c=1;c<15;c++)
{
HSSFCell celln=rown.getCell((短)c,rown.CREATE\u NULL\u AS\u BLANK);
如果((“出售给pt”).equals(celln.toString().trim())
{
coln=celln.getColumnIndex();
}
如果((“客户”).equals(celln.toString().trim())
{
coln1=celln.getColumnIndex();
}
如果((“采购订单号”)等于(celln.toString().trim())
{
coln2=celln.getColumnIndex();
}
如果((“采购订单日期”).equals(celln.toString().trim())
{
coln3=celln.getColumnIndex();
}
if(((“SO编号”).equals(celln.toString().trim())
{
coln4=celln.getColumnIndex();
}
if(((“SO Dtd”).equals(celln.toString().trim())
{
coln5=celln.getColumnIndex();
}
如果((“行项目”).equals(celln.toString().trim())
{
coln6=celln.getColumnIndex();
}
如果((“零件号”)等于(celln.toString().trim())
{
coln7=celln.getColumnIndex();
}
if((“Description”).equals(celln.toString().trim())
{
coln8=celln.getColumnIndex();
}
如果((“订单数量”)等于(celln.toString().trim())
{
coln9=celln.getColumnIndex();
}
如果((“区域”).equals(celln.toString().trim())
{
coln10=celln.getColumnIndex();
}
如果((“KAM”).equals(celln.toString().trim())
{
coln11=celln.getColumnIndex();
}
if(((“客户请求dt:).equals(celln.toString().trim()))
{
coln12=celln.getColumnIndex();
}
如果((“交货-全部/部分”).equals(celln.toString().trim())
{
coln13=celln.getColumnIndex();
}
}
对于(int r=1;r