2014-07-04 30 views
1
package Reader; 
     public class Write_Excel { 
      private WritableCellFormat timesBoldUnderline; 
      private WritableCellFormat times; 
      private String inputFile; 
      Thread thread = new Thread(); 
      static String box, plc, hmi; 
      int k=1,cnt=1; 

    public void witeExcel(ArrayList<String> B,ArrayList<String> P, ArrayList<String> H) 
        throws WriteException, IOException { 
       Write_Excel test = new Write_Excel(); 
       test.setOutputFile("C://Users//Tanmay//Desktop//Export.xls"); 
       for (int index = 0; index < B.size(); index++) { 
       box = B.get(index); 
       plc = P.get(index); 
       hmi = H.get(index); 

       test.write(); 
      }System.out.println("Please check the result file under C://Users//Tanmay//Desktop//Export.xls "); 
      } 

      public void setOutputFile(String inputFile) { 
       this.inputFile = inputFile; 
      } 

      public void write() throws IOException, WriteException { 
       File file = new File(inputFile); 
       WorkbookSettings wbSettings = new WorkbookSettings(); 
       wbSettings.setLocale(new Locale("en", "EN")); 
       WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings); 
       workbook.createSheet("Serial Numbers", 0); 
       WritableSheet excelSheet = workbook.getSheet(0); 
       excelSheet.setColumnView(1, 15); 
       excelSheet.setColumnView(2, 15); 
       excelSheet.setColumnView(3, 15); 
       createLabel(excelSheet); 
       createContent(excelSheet); 
       workbook.write(); 
       System.out.println("Value of k in close method is "+k); 
       if(k==4) { 
        System.out.println("Condition Satisfied where value is "+k); 
        workbook.close(); 
       }  
      } 

      private void createLabel(WritableSheet sheet) throws WriteException { 
       //System.out.println("In create label method"); 
       // Lets create a times font 
       WritableFont times10pt = new WritableFont(WritableFont.ARIAL, 12); 
       // Define the cell format 
       times = new WritableCellFormat(times10pt); 
       // Lets automatically wrap the cells 
       times.setWrap(true); 

       // create create a bold font with underlines 
       WritableFont times10ptBoldUnderline = new WritableFont(
         WritableFont.TIMES, 12, WritableFont.BOLD, false); 
       timesBoldUnderline = new WritableCellFormat(times10ptBoldUnderline); 
       // Lets automatically wrap the cells 
       timesBoldUnderline.setWrap(false); 

       CellView cv = new CellView(); 
       cv.setFormat(times); 
       cv.setSize(20); 
       cv.setFormat(timesBoldUnderline); 

       // Write a few headers 
       addCaption(sheet, 0, 0, "BOX"); 
       addCaption(sheet, 1, 0, "CPU"); 
       addCaption(sheet, 2, 0, "HMI"); 
       //System.out.println("Out create label method"); 
      } 

      private void addCaption(WritableSheet sheet, int column, int row, String s) 
        throws RowsExceededException, WriteException { 
       //System.out.println("In addCaption Method"); 
       Label label; 
       label = new Label(column, row, s, timesBoldUnderline); 
       sheet.addCell(label); 
       //System.out.println("Out addCaption Method"); 
      } 

      private void createContent(WritableSheet sheet) throws WriteException, 
        RowsExceededException { 
       System.out.println("In createContent Method"); 
        addLabel(sheet, 0, k, box); 
        addLabel(sheet, 1, k, plc); 
        addLabel(sheet, 2, k, hmi); 
        //System.out.println("Value of K is "+k); 
        System.out.println("Out createContent Method"); 
        k++; 
      } 

      private void addLabel(WritableSheet sheet, int column, int row, String s) 
        throws WriteException, RowsExceededException { 
       System.out.println("In Addlabel Method"); 
       Label label; 
       label = new Label(column, row, s, times); 
       System.out.println("Value of row is "+row+" Value of column is "+column+" Value string is "+s); 
       sheet.addCell(label); 
       System.out.println("Out addlabel Method"); 
      } 
     } 

我建立了条形码读取的应用程序现在与下面的代码的问题是,当我得到擅长的O/P仅最后一个数据文件,它是采取以表格的形式得到在o/p的excel文件中。我需要一些建议,我在哪里出错,应该做什么。我已经使用system.out.printlln()检查了所有内容。但没有找到任何解决方案。Java文件只有最后的数据得到插入文件

Thanks in advance and double to one who helps to get right solution. 
+0

要创建每次您要添加的行中的新文件。你需要在构造函数中创建你的文件。那么在你的写入方法中不要创建文件 – nafas

+0

我已经尝试过使用文件在构造函数中创建,但工作簿设置正在创建该问题。你能帮助我吗? – user3793580

+0

检查vinayknl的答案。这应该为你工作 – nafas

回答

0

原因是您每次打电话时都会在方法中创建工作簿,以便旧数据得到清理并添加新数据。

将其从方法中移除并移至其他位置执行一次。

File file = new File(inputFile);//From File 
    WorkbookSettings wbSettings = new WorkbookSettings(); 
    wbSettings.setLocale(new Locale("en", "EN")); 
    WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings); 
    workbook.createSheet("Serial Numbers", 0);/To configuration 

我建议你将你的初始化移动到构造函数。

  • 全部移动初始化构造器OR
  • 一流水平初始化元素writeExcel声明这些变量意味着,你必须声明你variables.So,你可以初始化那些wordExcel
+0

但是如何处理文件文件。它在创建错误的地方声明它。 – user3793580

+0

File file = new File(inputFile);和WritableWorkbook工作簿= Workbook.createWorkbook(file,wbSettings);正在声明错误 – user3793580

2
 public void witeExcel(ArrayList<String> B,ArrayList<String> P, ArrayList<String> H) 
       throws WriteException, IOException { 
      WorkbookSettings wbSettings = new WorkbookSettings(); 
      wbSettings.setLocale(new Locale("en", "EN")); 
      WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings); 
      workbook.createSheet("Serial Numbers", 0); 

      Write_Excel test = new Write_Excel(); 
      test.setOutputFile("C://Users//Tanmay//Desktop//Export.xls"); 
      for (int index = 0; index < B.size(); index++) { 
      box = B.get(index); 
      plc = P.get(index); 
      hmi = H.get(index); 

      test.write(workbook); 
     }System.out.println("Please check the result file under C://Users//Tanmay//Desktop//Export.xls "); 
     } 

移动逻辑循环

更改写方法之前创建工作簿传递工作簿

public void write(WritableWorkbook workbook) throws IOException, WriteException { 
      File file = new File(inputFile); 


      WritableSheet excelSheet = workbook.getSheet(0); 
     ................ 
+0

这是最有可能的工作。 – nafas

+0

@TAsk它已被作为参数传递,我不明白为什么它不应该通过工作簿 – nafas

+0

写入方法。 – vinayknl

相关问题