Apache POI - 移除外部工作簿引用
创始人
2024-09-04 15:30:12
0

要移除Apache POI中的外部工作簿引用,可以使用HSSF(用于处理Excel 97-2003格式)或XSSF(用于处理Excel 2007及更高版本)类库中的相应方法。以下是使用Apache POI的示例代码:

使用HSSF类库:

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Name;

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

public class RemoveExternalWorkbookReference {

    public static void main(String[] args) {
        try {
            // 读取Excel文件
            FileInputStream file = new FileInputStream("path/to/input-file.xls");
            Workbook workbook = new HSSFWorkbook(file);

            // 获取所有的命名区域
            int numberOfNames = workbook.getNumberOfNames();
            for (int i = 0; i < numberOfNames; i++) {
                Name name = workbook.getNameAt(i);
                String refersToFormula = name.getRefersToFormula();

                // 判断是否是外部工作簿引用
                if (refersToFormula.startsWith("[")) {
                    // 移除外部工作簿引用
                    name.setRefersToFormula("#REF!");
                }
            }

            // 保存修改后的Excel文件
            FileOutputStream out = new FileOutputStream("path/to/output-file.xls");
            workbook.write(out);
            out.close();

            System.out.println("外部工作簿引用已移除成功");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

使用XSSF类库:

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Name;

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

public class RemoveExternalWorkbookReference {

    public static void main(String[] args) {
        try {
            // 读取Excel文件
            FileInputStream file = new FileInputStream("path/to/input-file.xlsx");
            Workbook workbook = new XSSFWorkbook(file);

            // 获取所有的命名区域
            int numberOfNames = workbook.getNumberOfNames();
            for (int i = 0; i < numberOfNames; i++) {
                Name name = workbook.getNameAt(i);
                String refersToFormula = name.getRefersToFormula();

                // 判断是否是外部工作簿引用
                if (refersToFormula.startsWith("[")) {
                    // 移除外部工作簿引用
                    name.setRefersToFormula("#REF!");
                }
            }

            // 保存修改后的Excel文件
            FileOutputStream out = new FileOutputStream("path/to/output-file.xlsx");
            workbook.write(out);
            out.close();

            System.out.println("外部工作簿引用已移除成功");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

请确保将path/to/input-file.xlspath/to/output-file.xls(或path/to/input-file.xlsxpath/to/output-file.xlsx)替换为您实际的文件路径。运行代码后,将从输入文件中移除所有外部工作簿引用,并将结果保存到输出文件中。

请注意,这些示例代码仅移除命名区域中的外部工作簿引用。如果您的工作簿中还有其他类型的引用(例如公式),您可能需要根据具体情况进行修改。

相关内容

热门资讯

今日公布!微扑克辅助挂(weP... 今日公布!微扑克辅助挂(wePOke)外挂透明挂辅助工具(辅助挂)曝光教程(有挂分析)-哔哩哔哩;亲...
玩家必看科普(扑克时间后台数据... 玩家必看科普(扑克时间后台数据)外挂透明挂辅助挂(辅助挂)发牌机制(有挂秘笈)-哔哩哔哩;最新版20...
重大通报(pokerx人工智能... 重大通报(pokerx人工智能)外挂透明挂辅助app(辅助挂)辅助透视(2023已更新)(哔哩哔哩)...
透视计算!aapoker工具(... 透视计算!aapoker工具(wEpOke)外挂透明挂辅助软件(辅助挂)攻略教程(有挂攻略)-哔哩哔...
一分钟揭秘(wpk系统)外挂透... 一分钟揭秘(wpk系统)外挂透明挂辅助机制(辅助挂)透视辅助(有挂技术)-哔哩哔哩;小薇(透视辅助)...
总算了解(德扑之星ai代打)外... 总算了解(德扑之星ai代打)外挂透明挂辅助插件(辅助挂)软件透明挂(2024已更新)(哔哩哔哩);科...
透视挂(德州游戏)外挂透明挂辅... 透视挂(德州游戏)外挂透明挂辅助软件(辅助挂)透视辅助(有挂秘笈)-哔哩哔哩;科技详细教程小薇《75...
每日必看!德扑之星专业(WEP... 每日必看!德扑之星专业(WEPOke)外挂透明挂辅助APP(透视)安装教程(讲解有挂)-哔哩哔哩;德...
透视辅助(德扑机器人)外挂透明... 透视辅助(德扑机器人)外挂透明挂辅助挂(透视)软件透明挂(2025已更新)(哔哩哔哩);玩家必备必赢...
玩家实测(德州免费)外挂透明挂... 玩家实测(德州免费)外挂透明挂辅助工具(辅助挂)发牌规律(果真有挂)-哔哩哔哩;玩家必备必赢加哟《1...