gpt4 book ai didi

java - 创建 Excel 文件的副本未按预期工作

转载 作者:行者123 更新时间:2023-12-01 09:16:55 25 4
gpt4 key购买 nike

我想在替换 "' 后将一个 excel(.xls) 的全部内容复制到另一个 excel(.xls) 中。但是,此代码正在创建一个新的 excel,但仅复制最后一列(在我的例子中,是第七列)。请告诉我这段代码哪里出了问题...

公共(public)类重命名{

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();

}

}

谢谢

最佳答案

问题出在以下几行..

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();
}
}

关于java - 创建 Excel 文件的副本未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40483543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com