我有以下代码填写Excel文件,其中包含我使用Jsoup从Internet获得的信息.
package knvbj;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.TextNode;
import org.jsoup.select.Elements;
public class KNVBJ {
private static int Clnummer=1;
public static void main(String[] args) throws IOException {
FileOutputStream out = new FileOutputStream("/Users/muratcanpinar/Downloads/KNVBJ/build/classes/knvbj/ClubInformation.xlsx");
List<String> urlList = ReadXlsx.readXlsx();
urlList.get(1);
for (String url : urlList) {
System.out.println("url: " + url);
}
for (int i = 0; i < urlList.size(); i++) {
Document doc …Run Code Online (Sandbox Code Playgroud) 我可以写前X行,但是当我尝试写另外X行时,却遇到异常-
org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException:保存失败:保存程序包时发生错误:/docProps/app.xml未能使用编组org.apache.poi.openxml4j.opc保存在流中.internal.marshallers.DefaultMarshaller @ 6825c828
。
public class ExcellTest
{
XSSFWorkbook workbook;
XSSFSheet sheet;
int rowNum = 0;
String excellFileName = "";
FileOutputStream fileOut;
public ExcellTest(String excellFileName) {
createExcellSheet("test");
this.excellFileName = excellFileName;
}
public void createExcellSheet(String sheetName) {
workbook = new XSSFWorkbook();
sheet = workbook.createSheet(sheetName);
}
public void addData() {
Row rowMsg = sheet.createRow((short)rowNum);
rowNum++;
Cell cell;
for (int i = 0; i < 200; i++) {
cell = rowMsg.createCell(i);
cell.setCellValue(rowNum);
}
}
public void createExcellFile() {
try { …Run Code Online (Sandbox Code Playgroud) 我正在尝试从 Excel 读取测试数据并使用测试结果更新同一张表。问题是,在第二次迭代时,Eclipse IDE 会生成异常,并且 excel 中的状态也不会更新。该文件也似乎已损坏。这是代码:
File src = new File("C:\\Users\\Sajid\\Desktop\\SeleniumData.xlsx");
FileOutputStream fos = new FileOutputStream(src, true);
FileInputStream fis = new FileInputStream(src);
workbook = new XSSFWorkbook(fis);
sheet = workbook.getSheetAt(0);
for (int i = 1; i <= sheet.getLastRowNum(); i++)
{
// Import data for Email.
cell = sheet.getRow(i).getCell(0);
cell.setCellType(Cell.CELL_TYPE_STRING);
new WebDriverWait(driver, 50).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"email\"]")));
driver.findElement(By.xpath("//*[@id=\"email\"]")).sendKeys(cell.getStringCellValue());
String message = "Pass";
sheet.getRow(i).createCell(15).setCellValue(message);
workbook.write(fos);
}
Run Code Online (Sandbox Code Playgroud)
例外情况:
org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException:保存失败:保存包时发生错误:部分 /docProps/app.xml 无法使用编组器 org.apache.poi.openxml4j.opc 保存在流中.internal.marshallers.DefaultMarshaller@2e62e227
更新代码
File src = new File("C:\\Users\\Sajid\\Desktop\\SeleniumData.xlsx");
FileOutputStream fos = new FileOutputStream(src, true);
FileInputStream fis …Run Code Online (Sandbox Code Playgroud)