2016-11-08 151 views
0

我想更換後一個Excel文件(.xls的)到另一個的全部內容進行復制。然而,這段代碼是創建一個新的Excel但只複製最後一列(在我的情況,這是第7列)。請指點我在哪裏與此代碼會錯...創建一個Excel文件的副本無法正常運行

公共類重命名{

static HSSFRow row_read = null; 
static HSSFRow row_write = null; 
static Cell cell; 
static FileOutputStream output = null; 
static HSSFWorkbook workbook_read = null; 
static HSSFWorkbook workbook_write = null; 
static HSSFSheet sheet_read = null; 
static HSSFSheet sheet_write = null; 
public static void removechar() 
{ 
try{ 
FileInputStream input = new FileInputStream("inputpath//test_input.xls"); 
workbook_read = new HSSFWorkbook(input); 
sheet_read = workbook_read.getSheet("Report"); 
workbook_write = new HSSFWorkbook(); 
sheet_write = workbook_write.createSheet("Test"); 
DataFormatter formatter = new DataFormatter(); 
int rowCount = sheet_read.getLastRowNum(); 
System.out.println(rowCount); 
for(int rowNum = 0; rowNum<=rowCount; rowNum++) 
{ 
for(int cellNum = 0; cellNum<=7; cellNum++) 
{ 
//Cell cell = null; 
row_read=sheet_read.getRow(rowNum); 
row_write = sheet_write.createRow(rowNum); 
String temp = formatter.formatCellValue(row_read.getCell(cellNum)); 
//System.out.println(temp); 
String temp1 = temp.replaceAll("\'", ""); 
String temp2 = temp1.replaceAll("\"", ""); 
System.out.println(temp2); 
cell = row_write.createCell(cellNum); 
cell.setCellValue(temp2); 
} 
} 

output = new FileOutputStream("outputpath\\test_output.xls"); 
workbook_write.write(output); 

} 
catch(Exception e) 
{ 
    e.printStackTrace(); 
} 

} 
public static void main(String[] argv) throws IOException { 
    Rename.removechar(); 

} 

}

謝謝

回答

1

的問題是與以下行..

row_read=sheet_read.getRow(rowNum); 
row_write = sheet_write.createRow(rowNum); 

需要它是細胞循環(for(int cellNum = 0; cellNum<=7; cellNum++)

它應該看起來像外面。

for (int rowNum = 0; rowNum <= rowCount; rowNum++) { 
      row_read = sheet_read.getRow(rowNum); 
      row_write = sheet_write.createRow(rowNum); 
      for (int cellNum = 0; cellNum <= 7; cellNum++) { 
       // Cell cell = null; 
String temp = formatter.formatCellValue(row_read.getCell(cellNum)); 

也可能需要改變import static Cell cell;import HSSFCell cell;

下面是完整的代碼。

import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 

import org.apache.poi.hssf.usermodel.HSSFCell; 
import org.apache.poi.hssf.usermodel.HSSFRow; 
import org.apache.poi.hssf.usermodel.HSSFSheet; 
import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
import org.apache.poi.ss.usermodel.DataFormatter; 

public class Rename { 


    static HSSFRow row_read = null; 
    static HSSFRow row_write = null; 
    static HSSFCell cell; 

    static FileOutputStream output = null; 
    static HSSFWorkbook workbook_read = null; 
    static HSSFWorkbook workbook_write = null; 
    static HSSFSheet sheet_read = null; 
    static HSSFSheet sheet_write = null; 

    public static void removechar() { 
     try { 
      FileInputStream input = new FileInputStream("D://test_input.xls"); 
      workbook_read = new HSSFWorkbook(input); 
      sheet_read = workbook_read.getSheet("Report"); 
      workbook_write = new HSSFWorkbook(); 
      sheet_write = workbook_write.createSheet("Test"); 
      DataFormatter formatter = new DataFormatter(); 
      int rowCount = sheet_read.getLastRowNum(); 
      System.out.println(rowCount); 
      for (int rowNum = 0; rowNum <= rowCount; rowNum++) { 
       row_read = sheet_read.getRow(rowNum); 
       row_write = sheet_write.createRow(rowNum); 
       for (int cellNum = 0; cellNum <= 7; cellNum++) { 
        // Cell cell = null; 
        String temp = formatter.formatCellValue(row_read.getCell(cellNum)); 
        // System.out.println(temp); 
        String temp1 = temp.replaceAll("\'", ""); 
        String temp2 = temp1.replaceAll("\"", ""); 
        System.out.println(temp2); 
        cell = row_write.createCell(cellNum); 
        cell.setCellValue(temp2); 
       } 
      } 

      output = new FileOutputStream("D://test_output.xls"); 
      workbook_write.write(output); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 

    public static void main(String[] argv) throws IOException { 
     Rename.removechar(); 
    } 
} 
+0

此代碼更改只複製在新的Excel中的第一行.... :( – aman

+0

我已經添加了完整代碼的答案,它工作正常.. – Jobin

+0

謝謝......這像魅力一樣工作...... :) – aman