从excel获取输入并使用java将该数据传递给bat文件

时间:2017-06-20 06:25:23

标签: java excel batch-file

我想从excel读取数据并使用java将这些数据传递给批处理文件?

test.bat

  `@echo off`
echo /p id = enter your id

echo %id%

# Excel文件 1234

我们如何从excel文件中检索数据并使用java将这些数据传递给bat文件 有人可以回答这个问题吗?

2 个答案:

答案 0 :(得分:0)

您可以使用以下代码从Excel工作表中读取数据,然后将其写入另一个文件。

   try {

             InputStream input = new BufferedInputStream(
                        new FileInputStream("C:/MyWorkSpace/Temp/poi-test.xls"));
            POIFSFileSystem fs = new POIFSFileSystem( input );
            HSSFWorkbook wb = new HSSFWorkbook(fs);
            HSSFSheet sheet = wb.getSheetAt(0); //sheet of excel

            File file = new File("writedatasamp.txt");  
            writer = new BufferedWriter(new FileWriter(file));
            Iterator rows = sheet.rowIterator(); 
            while( rows.hasNext() ) {   
                HSSFRow row = (HSSFRow) rows.next();
                System.out.println("\n");
                Iterator cells = row.cellIterator();

                   writer.write("insert into Emp values(");
                while( cells.hasNext() ) {

                    HSSFCell cell = (HSSFCell) cells.next();
                    if(HSSFCell.CELL_TYPE_NUMERIC==cell.getCellType()) {
                    System.out.print( cell.getNumericCellValue()+"     "+cell.getColumnIndex() );
                    if(cell.getColumnIndex()==3)
                  writer.write(String.valueOf(cell.getNumericCellValue()));
                    else
                     writer.write(String.valueOf(cell.getNumericCellValue()+","));
                    }
                    else
                    if(HSSFCell.CELL_TYPE_STRING==cell.getCellType()) {
                        System.out.print( cell.getStringCellValue()+"     " );
                        writer.write("'"+cell.getStringCellValue()+"',");
                    }
                    else
                        if(HSSFCell.CELL_TYPE_BOOLEAN==cell.getCellType()) {
                        System.out.print( cell.getBooleanCellValue()+"     " );
                       writer.write("'"+String.valueOf(cell.getBooleanCellValue()+"',"));
                        }
                        else
                            if(HSSFCell.CELL_TYPE_BLANK==cell.getCellType())
                                System.out.print( "BLANK     " );
                                else
                            System.out.print("Unknown cell type");


                }

                writer.write(");"+"\n"); 
            }


        } catch ( IOException ex ) {
            ex.printStackTrace();
        } finally {             try {                 if (writer != null) {                     writer.close();                 }             } catch (IOException e) {                 e.printStackTrace();             }         }
    }

答案 1 :(得分:0)

如果您考虑使用excel文件执行任何操作,则应使用ApachePOI。很棒的图书馆可以轻松使用它!