Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用ApachePOI-Excel将数据写入xlsx文件时遇到的问题随后会损坏_Java_Excel_Apache Poi_Soapui_Xssf - Fatal编程技术网

Java 使用ApachePOI-Excel将数据写入xlsx文件时遇到的问题随后会损坏

Java 使用ApachePOI-Excel将数据写入xlsx文件时遇到的问题随后会损坏,java,excel,apache-poi,soapui,xssf,Java,Excel,Apache Poi,Soapui,Xssf,我删除了很多代码,主要是尝试catch块,并将我认为对您来说很容易看到的内容粘贴到这里,并就解决我面临的问题向我提供建议。我正在SoapUI工具中的Groovy脚本步骤中运行以下脚本。大多数变量数据类型都遵循Groovy语言语法 运行此代码时,将创建一个文件名为destPath变量中提到的xlsx文件,但其大小为0 KB。当我尝试打开文件时,我看到消息“Excel无法打开文件”“filename”,因为文件格式或文件扩展名无效。请验证文件是否已损坏,以及文件扩展名是否与文件格式匹配。“我看不到哪

我删除了很多代码,主要是尝试catch块,并将我认为对您来说很容易看到的内容粘贴到这里,并就解决我面临的问题向我提供建议。我正在SoapUI工具中的Groovy脚本步骤中运行以下脚本。大多数变量数据类型都遵循Groovy语言语法

运行此代码时,将创建一个文件名为destPath变量中提到的xlsx文件,但其大小为0 KB。当我尝试打开文件时,我看到消息“Excel无法打开文件”“filename”,因为文件格式或文件扩展名无效。请验证文件是否已损坏,以及文件扩展名是否与文件格式匹配。“我看不到哪里出错。”

def srcPath = "C:\\Test Data\\Test Data2.xlsx"
def destPath = "C:\\Test Data\\Results\\destSheet.xlsx"

def setData = new SetData(log,srcPath,destPath)

log.info setData.setCellData("Result",0,"Testing123")

class  SetData{
def log; //log variable to print values on the console
String srcPath; 
String destPath;
XSSFWorkbook workbook;
OPCPackage pkg;

SetData(log,srcPath,destPath){
this.log = log; 
this.srcPath =srcPath;
this.destPath = destPath}

public String setCellData(String colName,int rowNum, String data){

OPCPackage pkg = OPCPackage.open(srcPath);
Workbook workbook = WorkbookFactory.create(pkg);        
if(rowNum<0)
return "false"; 
int colNum=-1;
XSSFSheet sheet = workbook.getSheetAt(0);
XSSFRow row=sheet.getRow(0);
for(int i=0;i<row.getLastCellNum();i++){
if(row.getCell(i).getStringCellValue().trim().equals(colName))
colNum=i;           
}
sheet.autoSizeColumn(colNum);
row = sheet.getRow(rowNum+1);
log.info "Row data " + row  //log.info is equivalent to System.out.Println
in Java,to print values on the console.

XSSFCell cell = row.getCell(colNum);

// cell style
CellStyle cs = workbook.createCellStyle();
cs.setWrapText(true);
cell.setCellStyle(cs);
log.info data; //prints Testing123
cell.setCellValue(data); // Set the cell data
log.info "raw cell Value" + "***" +  
cell.getRichStringCellValue().getString() //Prints "Testing123"
log.info "Column index "+ cell.getColumnIndex() // Prints 3
log.info cell.getRowIndex() // Prints 0
fileOut = new FileOutputStream(new File(destPath)); 
workbook.write(fileOut);
fileout.flush();
fileOut.close();
fileOut=null;
pkg.close()
return "true";
}
def srcPath=“C:\\Test Data\\Test Data2.xlsx”
def destPath=“C:\\Test Data\\Results\\destSheet.xlsx”
def setData=新setData(日志、srcPath、destPath)
log.info setData.setCellData(“结果”,0,“测试123”)
类集合数据{
def log;//用于在控制台上打印值的日志变量
字符串路径;
字符串路径;
XSSF工作手册;
OPCPackage-pkg;
SetData(日志、srcPath、destPath){
this.log=log;
this.srcPath=srcPath;
this.destPath=destPath}
公共字符串setCellData(字符串colName、int rowNum、字符串数据){
OPCPackage pkg=OPCPackage.open(srcPath);
工作簿=WorkbookFactory.create(pkg);

if(rowNum看起来像您的XSSF工作簿和工作簿。您还应该检查从源文件中读取的行和单元格是否确实包含这些行/单元格,如果没有,则创建它们。下面是一个简化的代码版本,用于从另一个工作簿创建Excel工作簿并写入其中:

    String srcPath = "C:\\projects\\source.xlsx";
    String destPath = "C:\\projects\\destSheet.xlsx";

    XSSFWorkbook workbook = (XSSFWorkbook)WorkbookFactory.create(new File(srcPath));        

    XSSFSheet sheet = workbook.getSheetAt(0);

    XSSFRow row = sheet.getRow(1);
    if(row == null) { row = sheet.createRow(1); }

    XSSFCell cell = row.getCell(0);
    if(cell == null) { cell = row.createCell(0); }

    cell.setCellValue("Testing123");

    FileOutputStream fileOut = new FileOutputStream(new File(destPath)); 
    workbook.write(fileOut);
    fileOut.flush();
    fileOut.close();

任何人都可以提供帮助吗?嗨,Phill,谢谢你的回复。它现在运行良好。但是,我想就你的答案提几点。1.混合使用工作簿和XSSFWorkbook并没有导致我遇到的问题。2.我在代码中没有提到fileout变量类型。如果你看我上面发布的代码,fileout变量将ld没有FileOutputStream类型。这就是发生此问题的原因。因为在我的原始代码中,我已将fileout语句包装在try catch块中,所以代码没有出错。现在,在将FileOutput变量类型指定为FileOutputStream后,它的工作方式就像一种款待。再次感谢Phil。您的代码为变量指定了FileOutStream类型abe归档并帮助我识别代码中的错误。