Reputation: 397
I'd created excel report(.xls) using Apache POI. Whenever I open it, it's showing a message "PROTECTED VIEW: Office has detected a problem with this file. Editing it may harm your computer". How to disable the protected view and Can I handle this within the code itself?
I'm getting the error message only when I apply the style to the cell.
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();
}
}
}
Upvotes: 0
Views: 4426
Reputation: 61975
So you are just another user of ancient versions of apache poi
. I would suggest you using the last stable version 3.17
instead of that 6 years old 3.9
.
So for all who will find this later: This code is using apache poi
version 3.10
or lower and will not more work in current versions.
And what do you expect the style.setBorderBottom(HSSFColor.TAN.index);
will do? The setBorderBottom
does setting the thickness of the border. This should be style.setBorderBottom(HSSFCellStyle.BORDER_THICK);
for example in your version. The setting the border color will be style.setBottomBorderColor(HSSFColor.TAN.index);
.
This is the problem. The int
HSSFColor.TAN.index
is 0x2f
and this is simply not allowed to be a border thickness. That's why Excel
denies using the file as a safe Excel
file.
So:
...
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);
...
Upvotes: 2