繁体   English   中英

如何为使用Apache POI生成的Excel文件禁用保护模式?

[英]How to disable protected mode for Excel file generated using Apache POI?

在此处输入图片说明 我已经使用Apache POI创建了excel report(.xls)。 每当我打开它时,它都会显示一条消息“保护视图:Office已检测到此文件的问题。对其进行编辑可能会损害您的计算机”。 如何禁用受保护的视图,我可以在代码本身内进行处理吗?

仅在将样式应用于单元格时才收到错误消息。

import java.io.File;
import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;

public class Test {

public static void main(String[] args) throws Exception {

    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = null;

    HSSFDataFormat dtFormat = wb.createDataFormat();
    HSSFRow row_data = null;
    HSSFCell cell_data = null;

    HSSFCellStyle style = null;
    HSSFCellStyle styleSubHeader = null;
    HSSFCellStyle styleLeft = null;
    HSSFCellStyle styleCentre = null;
    HSSFCellStyle styleBRData = null;
    HSSFCellStyle styleRight = null;
    HSSFCellStyle styleRPrec = null;
    HSSFCellStyle styleBold = null;
    HSSFCellStyle styleBRight = null;
    HSSFCellStyle styleBRPrec = null;
    HSSFCellStyle styleBLeftHead = null;
    HSSFCellStyle styleBLeft = null;
    HSSFCellStyle styleBCentre = null;
    HSSFCellStyle styleRRight = null;
    HSSFCellStyle styleSubTitle = null;
    HSSFFont headBold = null;
    HSSFFont titleBold = null;
    HSSFDataFormat dtFmt = null;

    HSSFCellStyle styleCenter = wb.createCellStyle();
    HSSFFont fontCenter = wb.createFont();
    HSSFFont font = wb.createFont();
    HSSFFont fontBold = wb.createFont();
    HSSFFont fontsubTitle = wb.createFont();

    FileOutputStream out = new FileOutputStream(new File("sample.xls"));

    try {

        styleSubTitle = wb.createCellStyle();
        sheet = wb.createSheet("Pricing Report");
        row_data = sheet.createRow(sheet.getLastRowNum());

        style = wb.createCellStyle();
        style.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
        style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        style.setBorderBottom(HSSFColor.TAN.index);
        style.setBorderLeft(HSSFColor.TAN.index);
        style.setBorderRight(HSSFColor.TAN.index);
        style.setBorderTop(HSSFColor.TAN.index);

        cell_data = row_data.createCell((short) 0);
        cell_data.setCellValue("Header 1");
        cell_data.setCellStyle(styleSubTitle);
        cell_data.setCellStyle(style);

        wb.write(out);

        out.close();

        System.out.println("Excel Generated");
    } catch (Exception e) {
        e.printStackTrace();
    }
 }

}

因此,您只是apache poi古代版本的另一个用户。 我建议您使用最新的稳定版本3.17而不是使用6年的3.9

因此,对于所有以后会发现此问题的人:该代码使用的是apache poi 3.10或更低版本,并且在当前版本中将无法使用。

您期望style.setBorderBottom(HSSFColor.TAN.index); 会做? setBorderBottom会设置边框的粗细。 这应该是style.setBorderBottom(HSSFCellStyle.BORDER_THICK); 例如您的版本。 设置边框颜色为style.setBottomBorderColor(HSSFColor.TAN.index);

这就是问题。 int HSSFColor.TAN.index0x2f并且完全不允许将其设为边框粗细。 这就是Excel拒绝将该文件用作安全Excel文件的原因。

所以:

...
        style = wb.createCellStyle();
        style.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
        style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        style.setBottomBorderColor(HSSFColor.TAN.index);
        style.setLeftBorderColor(HSSFColor.TAN.index);
        style.setRightBorderColor(HSSFColor.TAN.index);
        style.setTopBorderColor(HSSFColor.TAN.index);
        style.setBorderBottom(HSSFCellStyle.BORDER_THICK);
        style.setBorderLeft(HSSFCellStyle.BORDER_THICK);
        style.setBorderRight(HSSFCellStyle.BORDER_THICK);
        style.setBorderTop(HSSFCellStyle.BORDER_THICK);
...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM