Aspose cells 在保存wps编辑过的文件之后 里面的正文内容会变成蓝色字体

文件原本是xls格式的 保存为xlsx格式之后 使用wps编辑 再使用cell工具保存 会出现com.aspose.cells.b.a.d.k59 cannot be cast to com.aspose.cells.l86这个错误,如果保存为xls格式再使用wps编辑,再使用cell工具保存的时候不报错,但是里面的正文内容会部分变成蓝色字体

@qixinskys

Cause

The issue you’re experiencing with the text turning blue after saving an Excel file edited in WPS is likely related to how WPS handles formatting and styles when saving files in different formats. The error com.aspose.cells.b.a.d.k59 cannot be cast to com.aspose.cells.l86 indicates a type casting problem, which arises from incompatibilities between the formats or the way WPS modifies the document.

Solution

To avoid the blue font issue and the casting error, consider the following steps:

  1. Save as XLSX: When saving the file after editing in WPS, ensure that you save it in the XLSX format, as this format is more compatible with Aspose.Cells.
  2. Check for Formatting: Before saving the document with Aspose.Cells, check the formatting of the text in the workbook. You can reset the font color to the desired color programmatically.
  3. Error Handling: Implement error handling in your code to catch and manage the casting error gracefully.

Code

Here’s a sample code snippet to reset the font color before saving:

import com.aspose.cells.*;

public class ResetFontColor {
    public static void main(String[] args) {
        try {
            // Load the workbook
            Workbook workbook = new Workbook("path/to/your/file.xlsx");
            Worksheet worksheet = workbook.getWorksheets().get(0);

            // Reset font color for all cells
            for (int row = 0; row <= worksheet.getCells().getMaxDataRow(); row++) {
                for (int col = 0; col <= worksheet.getCells().getMaxDataColumn(); col++) {
                    Cell cell = worksheet.getCells().get(row, col);
                    Style style = cell.getStyle();
                    style.setFontColor(Color.getBlack()); // Set to black or desired color
                    cell.setStyle(style);
                }
            }

            // Save the workbook
            workbook.save("path/to/save/your/file.xlsx");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Next Steps

  1. Test the above code to see if it resolves the blue font issue.
  2. If the casting error persists, check the version of Aspose.Cells you are using and ensure it is up to date, as newer versions may have bug fixes related to format compatibility.
  3. If you continue to face issues, please provide more details about the specific versions of WPS and Aspose.Cells you are using.

Please implement these steps and let me know if you need further assistance!

我将xls保存为xlsx格式的目的是为了防止另外一个情况,我在使用复制sheet页的功能的时候,xls格式的文件会出现公式丢失的情况,而且你上述遍历单元格全部设置为黑色,会将我的原本是蓝色或者是别的颜色的字体也变成黑色。

@qixinskys
请提供你的xls原文件,xlsx文件,wps保存后的文件和可运行的测试代码。你可以将文件压缩成zip格式并上传到这里。我们很快就会检查。

文件.zip (323.8 KB)

文件在这个地方

@qixinskys,

感谢您提供的示例文件。

我使用您的示例 Excel 文件和示例代码测试了您的场景/案例。处理代码片段时没有出现任何异常或错误。

Workbook workbook = new Workbook("d:\\files\\被wps修改后的文件.xls");
workbook.save("d:\\files\\outputXLS.xls");
workbook.save("d:\\files\\outputXLSX.xlsx");

请在压缩包中查看附件中的输出 XLS 和 XLSX 文件。
files1.zip (334.4 KB)

当我将输出 XLSX 文件在 MS Excel 中打开时,MS Excel 提示错误消息。输出 XLS 文件在 MS Excel 中可以正常打开。请您分享一些细节,说明我们如何重现您提到的问题。此外,如果可能的话,请提供您的示例代码片段来生成您提到的问题。此外,请分享一些屏幕截图以突出显示问题。我们将尽快调查您的问题。

附言:我使用了最新版本的 Aspose.Cells for Java v25.6。

public static void main(String[] args) throws Exception {
    ExcelUtil.getLicense();
    String inputPath = "E:\\版本开发\\2025\\底稿的sheet页管理\\文件\\被wps修改过后的文件.xls";
    Workbook workbook = new Workbook(inputPath);

    // 刷新公式
    workbook.calculateFormula();


    workbook.save("E:\\齐心\\版本开发\\2025\\底稿的sheet页管理\\文件\\测试.xls", SaveFormat.XLSX);
    workbook.dispose();


}

这是我的代码 我使用的版本是23.4

@qixinskys,

感谢您提供的代码片段。我将您的代码片段与示例文件一起使用,效果相同,并且得到了与我之前帖子中附件相同的输出 XLS 和 XLSX 文件。
files2.zip (334.4 KB)

请试用 Aspose.Cells for Java v25.6,并告诉我们您的反馈。

@qixinskys
通过使用样例文件和以下样例代码在最新版本v25.6上进行测试,在保存xls文件到xlsx格式后,由于xlsx文件里包含了宏信息,导致结果文件打开时出现崩溃。在正常情况下,当把xls文件保存为xlsx格式后,文件里的宏相关的信息都会被移除掉。

Workbook workbook = new Workbook(filePath + "被wps修改过后的文件.xls");
workbook.calculateFormula();
workbook.save(filePath + "outputXLS_java.xls");
workbook.save(filePath + "outputXLSX_java.xlsx");
workbook.save(filePath + "outputXLSX_java.xlsm");

我们已经在内部问题跟踪系统中打开了以下新问题单,并将根据Free Support Policies中提到的条款提供修复。

问题单号:CELLSJAVA-46413

@qixinskys
你在wps编辑的时候, 加入一些宏函数:
<definedName name="SYML" comment="生成目录索引及链接">MID(GET.WORKBOOK(1),FIND("]",GET.WORKBOOK(1))+1,100)</definedName>

如果你想保留这些宏函数, 请保存成“.xlsm" 文件。
如果要存成xlsx 文件, 我们会试着删除这些宏函数

@qixinskys,

此外,我们想通知您,问题(Ticket:“CELLSJAVA-46413 - 将 xls 转换为 xlsx 时,由于宏异常消息导致结果文件崩溃”)已得到解决。此修复/增强功能将包含在计划于 2025 年 7 月上半月发布的版本 (Aspose.Cells v25.7) 中。下一版本发布后,我们将通知您。

The issues you have found earlier (filed as CELLSJAVA-46413) have been fixed in Aspose.Cells for Java 25.7.