I create an Excel file through Apache POI XSSF and I lock the sheet with a password so user can't change the value of the first two row and first five columns (I lock the sheet and allowed editing of other cells). All work fine, the only problem is that the user can't resize the column so he can neither change nor resize the columns to read all the cells value.
Is it possible to allow column resize even if the sheet is protected?
Thi is my configuration
workbook = new XSSFWorkbook();
sheet = workbook.createSheet("Sheet1");
sheet.protectSheet("passwordExcel");
unlockedNumericStyle = workbook.createCellStyle();
unlockedNumericStyle.setLocked(false);
// Format cell for date
dateStyle = workbook.createCellStyle();
CreationHelper createHelper = workbook.getCreationHelper();
dateStyle.setDataFormat(createHelper.createDataFormat().getFormat("dd/mm/yyyy"));
sheet.autoSizeColumn(1);
I read about lockFormatCell() but I don't understand if it can help me. Thanks
To be able resizing the column size while sheet is protected, you will need setting XSSFSheet.lockFormatColumns to false.
Complete example:
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
public class CreateExcelXSSFProtectedSheet {
public static void main(String[] args) throws Exception {
Workbook workbook = new XSSFWorkbook();
CreationHelper createHelper = workbook.getCreationHelper();
CellStyle unlockedNumericStyle = workbook.createCellStyle();
unlockedNumericStyle.setDataFormat(createHelper.createDataFormat().getFormat("$#,##0.00_);[Red]($#,##0.00)"));
unlockedNumericStyle.setLocked(false);
CellStyle dateStyle = workbook.createCellStyle();
dateStyle.setDataFormat(createHelper.createDataFormat().getFormat("dd/mm/yyyy"));
Sheet sheet = workbook.createSheet();
Row row = sheet.createRow(0);
Cell cell = row.createCell(1);
cell.setCellValue("some data");
row = sheet.createRow(1);
cell = row.createCell(1);
cell.setCellValue(-123456789.0123456);
cell.setCellStyle(unlockedNumericStyle);
row = sheet.createRow(2);
cell = row.createCell(1);
cell.setCellValue(new java.util.Date());
cell.setCellStyle(dateStyle);
((XSSFSheet)sheet).lockFormatColumns(false);
sheet.protectSheet("passwordExcel");
sheet.autoSizeColumn(1);
FileOutputStream out = new FileOutputStream("CreateExcelXSSFProtectedSheet.xlsx");
workbook.write(out);
out.close();
workbook.close();
}
}
Did style cell color && value right alignment like this:
XSSFColor color = new XSSFColor(new Color(43,150,150));
XSSFCellStyle cellStyle = myWorkBook.createCellStyle();
cellStyle.setFillForegroundColor(color);
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cellStyle.setAlignment(HorizontalAlignment.RIGHT);
//cellStyle.setAlignment(CellStyle.ALIGN_RIGHT);
Cell color working but cell value right alignment not working.
Your issue is not reproducible. Exact your code snippet works as expected using the current apache poi 4.1.1.
Let's have a Minimal, Reproducible Example to show that it really works.
Code:
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.xssf.usermodel.*;
class CreateExcelCellStyleAlingmentAndColor {
public static void main(String[] args) throws Exception {
try (XSSFWorkbook workbook = new XSSFWorkbook();
FileOutputStream fileout = new FileOutputStream("./Excel.xlsx") ) {
XSSFColor color = new XSSFColor(new java.awt.Color(43,150,150), null);
XSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFillForegroundColor(color);
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cellStyle.setAlignment(HorizontalAlignment.RIGHT);
XSSFSheet sheet = workbook.createSheet();
XSSFCell cell = sheet.createRow(0).createCell(0);
cell.setCellValue("A1");
cell.setCellStyle(cellStyle);
workbook.write(fileout);
}
}
}
Result:
You see, colored and right aligned.
I'm trying to create excel file with watermark in JAVA.
I'm using below approach:
1. using Apache POI api to create excel workbook
2. consume the poi workbook in aspose cells api for adding watermar.
When i'm trying to consume POI workbook in aspose cell i'm getting error- workbook not expected. Please help, as i'm new to spring/JAVA
PFB my code:
package com.mudassir.exceltest.testExcel;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Header;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Picture;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class TestExcelApplication {
private static String[] COLUMNs = {"Id", "Name", "Address", "Age"};
private static List<Customer> customers = Arrays.asList(
new Customer("1", "Jack Smith", "Massachusetts", 23),
new Customer("2", "Adam Johnson", "New York", 27),
new Customer("3", "Katherin Carter", "Washington DC", 26),
new Customer("4", "Jack London", "Nevada", 33),
new Customer("5", "Jason Bourne", "California", 36));
public static void main(String[] args) throws IOException {
Workbook workbook = new XSSFWorkbook();
CreationHelper createHelper = workbook.getCreationHelper();
Sheet sheet = workbook.createSheet("Customers");
Font headerFont = workbook.createFont();
headerFont.setBold(true);
headerFont.setColor(IndexedColors.BLUE.getIndex());
CellStyle headerCellStyle = workbook.createCellStyle();
headerCellStyle.setFont(headerFont);
// Row for Header
Row headerRow = sheet.createRow(0);
// Header
for (int col = 0; col < COLUMNs.length; col++) {
if(col== 0){
Cell cell = headerRow.createCell(col);
cell.setCellValue(COLUMNs[col]);
cell.setCellStyle(headerCellStyle);
}
else{
Cell cell = headerRow.createCell(col+1);
cell.setCellValue(COLUMNs[col]);
cell.setCellStyle(headerCellStyle);
}
}
// CellStyle for Age
CellStyle ageCellStyle = workbook.createCellStyle();
ageCellStyle.setDataFormat(createHelper.createDataFormat().getFormat("#"));
int rowIdx = 1;
for (Customer customer : customers) {
Row row = sheet.createRow(rowIdx++);
row.createCell(0).setCellValue(customer.getId());
row.createCell(2).setCellValue(customer.getName());
row.createCell(3).setCellValue(customer.getAddress());
Cell ageCell = row.createCell(4);
ageCell.setCellValue(customer.getAge());
ageCell.setCellStyle(ageCellStyle);
}
// read the image to the stream
final FileInputStream stream = new FileInputStream("image.png");
final CreationHelper helper = workbook.getCreationHelper();
final Drawing drawing = sheet.createDrawingPatriarch();
final ClientAnchor anchor = helper.createClientAnchor();
//anchor.setAnchorType( ClientAnchor.MOVE_AND_RESIZE );
final int pictureIndex =
workbook.addPicture(IOUtils.toByteArray(stream), Workbook.PICTURE_TYPE_PNG);
anchor.setCol1( 0 );
anchor.setRow1( 9 ); // same row is okay
anchor.setRow2( 11 );
anchor.setCol2( 2 );
final Picture pict = drawing.createPicture( anchor, pictureIndex );
//pict.resize();
Header header = sheet.getHeader();
header.setCenter("&[pict]");
header.setLeft("Left First Page Header");
header.setRight("Right First Page Header");
sheet.addMergedRegion(new CellRangeAddress(0,0,0,1));
sheet.addMergedRegion(new CellRangeAddress(1,1,0,1));
sheet.addMergedRegion(new CellRangeAddress(2,2,0,1));
sheet.addMergedRegion(new CellRangeAddress(3,3,0,1));
sheet.addMergedRegion(new CellRangeAddress(4,4,0,1));
com.aspose.cells.Workbook workbook1=new com.aspose.cells.Workbook(workbook);
FileOutputStream fileOut = new FileOutputStream("customerstest.xlsx");
workbook.write(fileOut);
fileOut.close();
workbook.close();
}
}
Please assist me on how can i consume POI workbook in aspose cell workbook.
Below code statement is not working, rather throwing type mismatch error:
com.aspose.cells.Workbook workbook1=new com.aspose.cells.Workbook(workbook);
Thanks
Well, Aspose.Cells and POI XSSF are different APIs with diverse architectures, both have different objects and attributes. I am not sure one can easily parse one's object in other APIs or may be he cannot do that. Aspose.Cells will read and parse valid Excel workbooks (which should follow MS Excel standards and specifications).
I think you may try to save your workbook to Excel file using POI XSSF APIs and then use Aspose.Cells to read that file. If the output file (by POI XSSF) follows MS Excel standards and specifications then it should be opened fine into MS Excel too. If it is opened fine into MS Excel then surely Aspose.Cells should also load the file fine. If you find any issue where Aspose.Cells could not read the final file, then it is an issue with Aspose.Cells. Otherwise I do not think it is an issue with Aspose.Cells. In short, you can simply save the Excel Workbook (by POI XSSF) to disk (Excel file) or streams first then use Aspose.Cells APIs to load it from disk or streams, it should work fine.
I am working as Support developer/ Evangelist at Aspose.
I create an Excel file through Apache POI XSSF and I lock the sheet with a password so user can't change the value of the first two row and first five columns (I lock the sheet and allowed editing of other cells). All work fine, the only problem is that the user can't resize the column so he can neither change nor resize the columns to read all the cells value.
Is it possible to allow column resize even if the sheet is protected?
Thi is my configuration
workbook = new XSSFWorkbook();
sheet = workbook.createSheet("Sheet1");
sheet.protectSheet("passwordExcel");
unlockedNumericStyle = workbook.createCellStyle();
unlockedNumericStyle.setLocked(false);
// Format cell for date
dateStyle = workbook.createCellStyle();
CreationHelper createHelper = workbook.getCreationHelper();
dateStyle.setDataFormat(createHelper.createDataFormat().getFormat("dd/mm/yyyy"));
sheet.autoSizeColumn(1);
I read about lockFormatCell() but I don't understand if it can help me. Thanks
To be able resizing the column size while sheet is protected, you will need setting XSSFSheet.lockFormatColumns to false.
Complete example:
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
public class CreateExcelXSSFProtectedSheet {
public static void main(String[] args) throws Exception {
Workbook workbook = new XSSFWorkbook();
CreationHelper createHelper = workbook.getCreationHelper();
CellStyle unlockedNumericStyle = workbook.createCellStyle();
unlockedNumericStyle.setDataFormat(createHelper.createDataFormat().getFormat("$#,##0.00_);[Red]($#,##0.00)"));
unlockedNumericStyle.setLocked(false);
CellStyle dateStyle = workbook.createCellStyle();
dateStyle.setDataFormat(createHelper.createDataFormat().getFormat("dd/mm/yyyy"));
Sheet sheet = workbook.createSheet();
Row row = sheet.createRow(0);
Cell cell = row.createCell(1);
cell.setCellValue("some data");
row = sheet.createRow(1);
cell = row.createCell(1);
cell.setCellValue(-123456789.0123456);
cell.setCellStyle(unlockedNumericStyle);
row = sheet.createRow(2);
cell = row.createCell(1);
cell.setCellValue(new java.util.Date());
cell.setCellStyle(dateStyle);
((XSSFSheet)sheet).lockFormatColumns(false);
sheet.protectSheet("passwordExcel");
sheet.autoSizeColumn(1);
FileOutputStream out = new FileOutputStream("CreateExcelXSSFProtectedSheet.xlsx");
workbook.write(out);
out.close();
workbook.close();
}
}
I want the code to export tables / reports to Excel and Word using POI.
I saw the examples that came with POI but could not understand. Can anyone provide me a small/easy code to do the same.
Seeing as you wanted actual code for using POI. Here is some to do some exporting:
import java.util.Date;
import java.util.List;
import java.util.ListIterator;
import java.util.StringTokenizer;
import java.io.*;
import org.apache.poi.hssf.usermodel.*;
public class XLSExporter implements Exporter {
/**
* Constructor for XLSExporter
*/
public XLSExporter(){
}
public void exportFile( File f, List o ) throws IOException{
HSSFWorkbook wb = new HSSFWorkbook();
FileOutputStream fileOut = new FileOutputStream(f);
HSSFSheet sheet = wb.createSheet();
ListIterator it = o.listIterator();
//Construct the headings
HSSFRow headingsRow = sheet.createRow((short)0);
//Heading format
HSSFFont headingFont = wb.createFont();
headingFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
HSSFCellStyle headingStyle = wb.createCellStyle();
headingStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
headingStyle.setFont(headingFont);
HSSFCell headingA = headingsRow.createCell((short)0);
headingA.setCellValue("Heading");
headingA.setCellStyle(headingStyle);
int i = 1;
// Iterate over the rows
while(it.hasNext()){
//Create the row
HSSFRow row = sheet.createRow((short)i);
//Write data
HSSFCell cellRunway = row.createCell((short)0);
cellRunway.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
cellRunway.setCellValue("Whateva");
cellRunway.setCellStyle(standardStyle);
i++;
}
//Set the column widths where needed
sheet.setColumnWidth((short)1, (short)4000);
wb.write(fileOut); // Write the workbook
fileOut.close();
}
}