标签: apache-poi

NotOLE2FileException: 无效的标头签名;读取 0x0000000000000000,预期为 0xE11AB1A1E011CFD0

我想要做的是询问用户是否要创建新的或选择现有的 Excel 工作簿。选择现有文件没有问题。但是,一旦我为新 Excel 文件创建名称,就会收到一条错误消息,提示“您的文件似乎不是有效的 OLE2 文档”。

public void selectExcelFile() {
    String excelFileName = null;                                // the name/directory/address of the excel file created/selected
    FileInputStream excelFileIn = null;                         // allows us to connect to the Excel file so we can read it
    FileOutputStream excelFileOut = null;                       // allows us to connect to the Excel file so we can write to it
    ExcelFileUtility eUtil = new ExcelFileUtility();            // used open an excel file

    if(columnsQuery != null) {
        try {
            excelFileName = eUtil.getFile(FileExtensions.XLS); …
Run Code Online (Sandbox Code Playgroud)

java apache excel apache-poi

3
推荐指数
1
解决办法
3万
查看次数

FileoutputStream FileNotFoundException

我正在使用 java SE eclipse。据我所知,当没有由参数命名的文件时 FileOutputStream 构造函数创建由参数命名的新文件。但是,随着继续,我看到 FileOutputStream 产生异常 FileNotFoundException。我真的不知道为什么需要这个例外。我的知识有问题吗?

我的代码如下(制作工作簿并写入文件。在此代码中,虽然没有文件“data.xlsx”,但 FileOutpuStream 制作文件“data.xlsx”。

    public ExcelData() {
    try {
        fileIn = new FileInputStream("data.xlsx");
        try {
            wb = WorkbookFactory.create(fileIn);
            sheet1 = wb.getSheet(Constant.SHEET1_NAME);
            sheet2 = wb.getSheet(Constant.SHEET2_NAME);
        } catch (EncryptedDocumentException | InvalidFormatException | IOException e) {
            e.printStackTrace();
        } // if there is file, copy data into workbook
    } catch (FileNotFoundException e1) {
        initWb();
        try {
            fileOut = new FileOutputStream("data.xlsx");
            wb.write(fileOut);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } 

    } // …
Run Code Online (Sandbox Code Playgroud)

java fileoutputstream apache-poi

3
推荐指数
1
解决办法
1万
查看次数

Java在Apache POI上进行搜索和替换时如何避免覆盖模板文件

我正在使用 Apache POI 3.13 并试图从给定的模板文件中搜索和替换文本,然后保存新生成的 .docx。这是我的代码:

public static void main(String[] args) throws InvalidFormatException, IOException {
    String filePath = "Sample.docx";
    File outputfile = new File("SampleProcessed.docx");

    XWPFDocument doc = new XWPFDocument(OPCPackage.open(filePath));

    for (XWPFParagraph p : doc.getParagraphs()) {
        List<XWPFRun> runs = p.getRuns();
        if (runs != null) {
            for (XWPFRun r : runs) {
                String text = r.getText(0);
                if (text != null && text.contains("$VAR")) {
                    text = text.replace("$VAR", "JohnDoe");
                    r.setText(text, 0);
                }
            }
        }
    }

    doc.write(new FileOutputStream(outputfile));
    doc.close();
    System.out.println("Done");
    Desktop.getDesktop().open(outputfile);
}
Run Code Online (Sandbox Code Playgroud)

这看起来很简单,但是当我运行这段代码时,文档“Sample.docx”也被替换了。最后,我有两个内容相同的文档。

这是 POI …

java apache-poi xwpf

3
推荐指数
1
解决办法
1105
查看次数

CTPageMar 值单位?

我正在使用 apache poi CTPageMar 类将页边距设置为用户给定的某个值。问题是我没有找到必须在函数 setLeft、setRight、setTop 和 setBottom 中传递的值的单位是什么。我试过厘米、像素、英寸,但它们似乎都错了。任何的想法?

XWPFDocument wordDocument = new XWPFDocument(new FileInputStream(input));
CTSectPr sectPr = wordDocument.getDocument().getBody().addNewSectPr();
CTPageMar pageMar = sectPr.addNewPgMar();
pageMar.setLeft(BigInteger.valueOf(left));
pageMar.setTop(BigInteger.valueOf(top));
pageMar.setRight(BigInteger.valueOf(right));
pageMar.setBottom(BigInteger.valueOf(bottom));
wordDocument.write(new FileOutputStream(output));
Run Code Online (Sandbox Code Playgroud)

java apache-poi xwpf

3
推荐指数
1
解决办法
943
查看次数

如何在Java,Apache POI中获取excel单元格字段的字体样式?

我想在 Java 中捕获 excel 中单元格字段的字体。我正在使用 Apache POI。如果可能的话,我想捕捉font-colorfont-familyfont-weightfont-size,等。

我怎样才能做到这一点?

java excel fonts apache-poi

3
推荐指数
1
解决办法
7241
查看次数

Apache POI - 将 Word 文档 (docx) 拆分为页面

我一直在尝试根据预定义的标准将 docx 文档分割为多个文档。以下是我将其切成段落的方法

        try {
        FileInputStream in = new FileInputStream(file);
        XWPFDocument doc = new XWPFDocument(in);
        List<XWPFParagraph> paragraphs = doc.getParagraphs();
        for (int idx = 0; idx < paragraphs.size(); idx++) {
            XWPFDocument outputDocument = new XWPFDocument();
            createParagraphInAnotherDocument(outputDocument, paragraphs.get(idx).getText());
            String fullPath = String.format("./content/output/%1$s_%2$s_%3$04d.docx", FileUtils.getFileName(file), getName(), idx);
            FileOutputStream outputStream = new FileOutputStream(fullPath);
            outputDocument.write(outputStream);
            outputDocument.close();

            doc.close();
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
Run Code Online (Sandbox Code Playgroud)

虽然我可以使用上面的代码提取段落,但我找不到提取页面的方法。我的理解是 word 中的页面是渲染关注的,它发生在 word 应用程序的运行时中。

java docx apache-poi xwpf

3
推荐指数
1
解决办法
4586
查看次数

Android:Apache POI 重复条目:org/apache/xmlbeans/xml/stream/Location.class 错误

嗨,我在运行我的 android 项目时遇到以下错误:

Error:Execution failed for task ':app:transformClassesWithJarMergingForDebug'.
> com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: org/apache/xmlbeans/xml/stream/Location.class
Run Code Online (Sandbox Code Playgroud)

我在互联网上搜索过这个问题,不同类型的解决方案提供了以下几个:

1] Enable multidex, (by doing `multiDexEnabled true`),
2] Remove support library v4 as v7 comes with it, (I am using only v7),
3] Increase jvm heap size through gradle or through gradle.properties,
2] Do not use multiple playstore library versions (Which I am not using already)
Run Code Online (Sandbox Code Playgroud)

当我在 gradle 中为 Apache POI 添加依赖项时,上述所有内容都开始了,如下所示:

dependencies {
    ....
    compile group: 'org.apache.poi', name: 'poi-ooxml', version: '3.14'
}
Run Code Online (Sandbox Code Playgroud)

以上都不适用于我的情况。为什么会发生这种情况以及什么是可靠的解决方案。

java android apache-poi android-gradle-plugin android-multidex

3
推荐指数
1
解决办法
7736
查看次数

Apache POI ClassNotFoundException

我正在使用 eclipse 尝试从 excel 电子表格中获取工作表列表,但是当我运行它时,Java 抛出异常。

这是代码;

File myFile = new File("excel.xlsx");
    Workbook wb = null;
    try {
        wb = WorkbookFactory.create(myFile);
    } catch (EncryptedDocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvalidFormatException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    List<String> sheetNames = new ArrayList<String>();
    for (int i=0; i<wb.getNumberOfSheets(); i++) {
        sheetNames.add( wb.getSheetName(i) );
    }
    System.out.println(sheetNames);
Run Code Online (Sandbox Code Playgroud)

这是日志;

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlObject
    at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:293) …
Run Code Online (Sandbox Code Playgroud)

java eclipse apache-poi

3
推荐指数
1
解决办法
3812
查看次数

避免公式注入,保留单元格值(HSSF/*.xls 中的引号前缀)

我正在开发的应用程序使用 Apache POI 创建 Excel 导出。通过安全审计,我们注意到如果用户不够小心,包含恶意值的单元格可能会产生任意进程。

要重现,请运行以下命令:

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

public class BadWorkbookCreator {
    public static void main(String[] args) throws Exception {
        try(
            Workbook wb = new HSSFWorkbook();
            FileOutputStream fos = new FileOutputStream("C:/workbook-bad.xls")
        ) {
            Sheet sheet = wb.createSheet("Sheet");
            Row row = sheet.createRow(0);
            row.createCell(0).setCellValue("Aaaaaaaaaa");
            row.createCell(1).setCellValue("-2+3 +cmd|'/C calc'!G20");
            wb.write(fos);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后打开生成的文件:

重现问题的截图

并按照以下步骤操作:

  1. 单击 (A) 以选择包含恶意内容的单元格
  2. 单击 (B) 使光标位于公式编辑器中
  3. 按 ENTER
  4. 系统会询问您是否允许 Excel 运行外部应用程序;如果您回答是,则 Calc 已启动(或任何恶意代码)

有人可能会说,用户负责让 Excel 运行任意的东西,用户被警告。但是,Excel 是从可信来源下载的,有人可能会落入陷阱。

使用 Excel,您可以在公式编辑器中的文本前放置单引号以将其转义。以编程方式在单元格内容中放置单引号(例如下面的代码)使单引号可见! …

java apache-poi

3
推荐指数
1
解决办法
6800
查看次数

Apache POI,CREATE_NULL_AS_BLANK 导致错误

现在我正在学习用 Java 编写数据驱动测试的教程。我的 IDE 是 IntelliJ 社区版,以下是我的代码

package utility;

import config.Constants;
import executionEngine.DriverScript;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

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

public class ExcelUtils {
    private static XSSFSheet ExcelWSheet;
    private static XSSFWorkbook ExcelWBook;
    private static XSSFCell Cell;
    private static XSSFRow Row;

    //This method is to set the File path and to open the Excel File
    //Pass Excel Path and Sheet Name as Arguments to this method
    public static void setExcelFile(String Path) throws Exception { …
Run Code Online (Sandbox Code Playgroud)

java apache intellij-idea data-driven-tests apache-poi

3
推荐指数
1
解决办法
7908
查看次数