2015-10-02 116 views
0

我已經編寫了一個代碼,它接收來自一個輸入excel文件的數據,並用所需的組織編寫一個新的輸出文件。我遇到的問題是,當我打開Java文件時,只有for循環中最新的列纔有數據。我已經通過在不同時間打破for循環來測試我的for循環是否是錯誤的。測試顯示它確實在正確的列中寫入了正確的數據。我不知道爲什麼它會選擇刪除前一列。是否有一行我缺少保存單元格值的方法,我只是爲了將數據保留在工作表上?使用Apache POI編寫excel文件,但FileOutput只包含一列

import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
import org.apache.poi.ss.usermodel.Workbook; 
import org.apache.poi.ss.usermodel.Sheet; 
import org.apache.poi.ss.usermodel.Row; 

import org.apache.poi.ss.usermodel.SheetConditionalFormatting; 
import org.apache.poi.ss.usermodel.ConditionalFormattingRule; 
import org.apache.poi.ss.util.CellRangeAddress; 
import org.apache.poi.ss.usermodel.Cell; 
import org.apache.poi.ss.usermodel.ComparisonOperator;//compare two different values 
import org.apache.poi.ss.usermodel.PatternFormatting; 
import org.apache.poi.ss.usermodel.IndexedColors; 


import java.util.Iterator; 
import java.io.*; 
import javax.swing.JFileChooser; 

public class Reorganizer 
{ 
public static void main(String[] args) 
{ 
    /*File Chooser*/ 
    JFileChooser fileChooser = new JFileChooser();//lets user choose file 
    int returnValue = fileChooser.showOpenDialog(null);//null bc we don't have parent classes, lets user choose through an open window 

    if(returnValue == JFileChooser.APPROVE_OPTION) 
    { 
     try 
     { 
      Workbook wb= new HSSFWorkbook(new FileInputStream(fileChooser.getSelectedFile()));//setting the input file as a workbook 
      Sheet inputSheet = wb.getSheetAt(0);//the sheet from the input file 

      Workbook wb1 = new HSSFWorkbook();//creating a new workbook to later be put into the output file 
      Sheet outputSheet = wb1.createSheet("Sheet 1");//creates new sheet on the output excel file 

      //first need something to find cell# of the file 
      Row idRow = inputSheet.getRow(0); 
      int cellCounter=0; 
      for(Iterator<Cell> cit = idRow.cellIterator(); cit.hasNext();)//goes down the row, until there is nothing 
      {     
       Cell cell = cit.next(); 
       cellCounter++; 
      } 
      //finds the number of rows 
      int rowCounter=0; 
      for(Iterator<Row> rit = inputSheet.rowIterator();rit.hasNext();)//goes down the column, until there is nothing 
      {     
       Row row = rit.next(); 
       rowCounter++; 
      } 


      //The first row of the document should be the labeling process 
      //Set the row(0) and the cells(9) 
      Row labelRow = outputSheet.createRow(0); 
      //for loop to make 9 cells with labels 
      String[] cellValues = new String[]{"Chr","Pos","Olfr","Subfamily","Cluster","OP6u","OP6d","OP27u","OP27d"}; 
      for(int i=0; i<=9;i++) 
      { 
       Cell labelCells = labelRow.createCell(i); 
       if(i<=8) 
       { 
        labelCells.setCellValue(cellValues[i]); 
        for(Iterator<Cell> cit = idRow.cellIterator(); cit.hasNext();) 
         { 
          Cell cell = cit.next(); 
          cell.setCellType(Cell.CELL_TYPE_STRING); 
          if(cellValues[i].equals(cell.getStringCellValue())) 
          { 
           for(int j=1;j<rowCounter;j++) 
           { 
            int cellnum = cell.getColumnIndex(); 
            Cell inputCell = inputSheet.getRow(j).getCell(cellnum); 
            inputCell.setCellType(Cell.CELL_TYPE_STRING); 
            String data = inputCell.getStringCellValue(); 
            Cell outputCell = outputSheet.createRow(j).createCell(i); 
            outputCell.setCellValue(data); 
            System.out.println(cellnum);//tells me where the input column was from 
            System.out.println(i);////tells me what row cell I'm writing on 
            System.out.println(j);//tells me what row number 
            System.out.println(data);//tells me the data being inputed into file 
//used the above prints to find out what it was doing, 
//this is the code used to write the data 


           } 
          } 

         } 
       } 
       else 
       { 
        labelCells.setCellValue(""); 
       } 

      } 

     FileOutputStream output = new FileOutputStream("Reorganized.xls"); 
     wb1.write(output); 
     output.flush(); 
     output.close(); 
     } 
     catch (FileNotFoundException e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     catch (IOException e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 


    } 

} 

} 

回答

1

看起來像你的問題在這裏。當您調用createRow()時,它將清除現有數據。

Cell outputCell = outputSheet.createRow(j).createCell(i); 

您可以預先創建的所有行您填充數據,也可以調用的getRow()之前進行測試,如果該行已經存在。你需要像這樣的東西:

Row row = outputSheet.getRow(j); 
if (row == null) { 
    row = outputSheet.createRow(j); 
} 
Cell outputCell = row.createCell(i); 
相關問題