I'm creating an Excel file where, once created and downloaded, a user isn't allowed to let empty cells in a specific column (because he will send it again with information he entered).
I'm using POI HSSFDataValidation with setEmptyCellAllowed(false).
But when the user downloads the file, he still can leave empty cells (after writing some text and deleting it).
Any suggestions?
Here's my code:
HSSFDataValidation dv = new HSSFDataValidation();
dv.setFirstColumn((short)19);
dv.setLastColumn((short)19);
dv.setFirstRow((short)4);
dv.setLastRow((short)24);
dv.setDataValidationType(DVConstraint.ValidationType.INTEGER);
dv.setOperator(DVConstraint.OperatorType.BETWEEN);
dv.setDataValidationType(HSSFDataValidation.DATA_TYPE_INTEGER);
dv.setOperator(HSSFDataValidation.OPERATOR_BETWEEN);
dv.setFirstFormula("0");
dv.setSecondFormula("1000");
//dv.setEmptyCellAllowed(true);
dv.setEmptyCellAllowed(false);
dv.setShowPromptBox(true);
dv.setSurppressDropDownArrow(false);
dv.setErrorStyle(HSSFDataValidation.ERROR_STYLE_STOP);
//dv.createErrorBox("", "");
//dv.createPromptBox("", "");
sheet.addValidationData(dv);
this is not answering your question directly but you can use a try and catch block to write something in the cell yourself if the user has kept it blank:
Related
In my anylogic model my agents receive their parameters from a database table, which is based on an Excel file. In the Excel file, each cell has its own code stored, so each time I open the file, the cell values change.
I would like to have that with each automatic run of my model the Excel file is read in again as the database table (i.e. the parameter values of the agents change).
In the "Parameter Variation Experiment" I entered this code under "after iteration":
String tempString = excelFile_DatabasisLinks.getCellStringValue(1, rowCounter, 1);
ModelDatabase modelDB = getEngine().getModelDatabase();
Database myNewFile = new Database(this, "rohdaten2", tempString);
modelDB.importFromExternalDB(myNewFile.getConnection(), "Rohdaten", "rohdaten", true, false);
rowCounter += 1;
I have the code form this anylogic help page. Using a variable to be able to change the path of the file (i.e. the file) seems to work (anylogic doesn't throw an error).
In the currently used dummy model, the agents receive their parameters at the source.
At the sink, the parameter values are written via collections into another excel (results) file.
I put obvious pattern into my data files, to see if the data changes, but I always receive the same excel file in my results file.
I read that anylogic copies the excel tables to its temporary files to make simulation runs faster. I hoped the code above would be a workaround, but it is not.
I'm grateful for any suggestions how to make this work!
I could not find out what is wrong with the above code or how to get it work.
However, I found a workaround using the "excelFile "-Block.
In the main agent (the agent where all other agents live) in the agent actions in "on startup":
excelFile.readFile();
ensures that the excel file is updated before each run. The parameters are added via
agent.set_<parametername>(excelFile.getCellBooleanValue( sheet number, row number, column number)
"on exit" in the source.
I hope this helps everyone with a similar problem.
I need to do the following:
User downloads excel file (which is a template) with some columns read only and other editable;(read only columns were made by protecting entire sheet
//protect entire sheet
sheet.protectSheet("password");
//create style for editable cells
XSSFCellStyle editableStyle = workbook.createCellStyle();
editableStyle.setLocked(false);
//for editable cells apply this style
cell.setCellStyle(editableStyle);
User modifies the template file, filling only editable cells
User upload the template
During uploading the template I need to check if the uploaded file is the one downloaded before, protected with exactly the same "password".
I have the possibility to get the password but it is encoded and I don't know how to decode it.
byte[] password = sheet.getCTWorksheet().getSheetProtection().getPassword();
Can you please help me?
If you want to check if the file is have the same password, then you can use validateSheetPassword method to check it.
Example:
if (sheet.validateSheetPassword("password"))
print("It same password");
Or you can see the documentation here: https://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFSheet.html#validateSheetPassword(java.lang.String)
I'm using Apache POI 3.12 (SXSSF workbook) in order to generate .xlsx files.
The problem is that I'm doing the generation and when I open the file I'm receiving an error message:
Excel found unreadable content in file.xlsx. Do you want to recover
the contents of this workbook? If you trust the source of this
workbook, click Yes.
After clicking Yes, the file opens and I'm receiving this notification
Excel completed file level validation and repair. Some parts of this
workbook may have been repaired or discarded. Removed Records:
Comments from /xl/comments1.xml part (Comments) Repaired Records:
Comments from /xl/comments1.xml part (Comments)
After that, I unzip the excel file and check the comments1.xml. All my comments are present. All 216 of them.
The section of the code that generates the comments is the following
String comment = _propertiesHolder.getComment();
String commentAuthor = _propertiesHolder.getCommentAuthor();
if(comment != null)
{
int colIndex = cell.getColumnIndex();
int rowIndex = cell.getRowIndex();
CreationHelper helper = _workbook.getCreationHelper();
ClientAnchor anchor = helper.createClientAnchor();
anchor.setCol1(colIndex);
anchor.setCol2(colIndex + 1);
anchor.setRow1(rowIndex);
anchor.setRow2(rowIndex + 3);
// Create the comment and set the text+author
Comment cellComment = _drawingPatriarch.createCellComment(anchor);
if(commentAuthor != null)
{
cellComment.setAuthor(commentAuthor);
RichTextString rs = helper.createRichTextString(commentAuthor + ": " + comment);
cellComment.setString(rs);
}
else
{
cellComment.setString(helper.createRichTextString(comment));
}
cellComment.setRow(rowIndex);
cellComment.setColumn(colIndex);
// Assign the comment to the cell
cell.setCellComment(cellComment);
}
Do you have any idea what could be the cause of this problem? Although no information was lost, clearly there is something wrong and I would like to fix it. The comments are retrieved from database (varchar datatype). The biggest comment is 138 characters long.
Update
Something that I forgot to mention. I've also run the same extraction using hssf implementation and no errors were present. It would be a safe assumption that the data are not the problem.
Ok I found the problem. It was with the author.
The problem is this line cellComment.setAuthor(commentAuthor);.
If for one comment we set
cellComment.setAuthor("test")
and then in another comment we set
cellComment.setAuthor("test ")
There will be an error shown when opening the file. Mind the whitespace. The solution is to trim the author string before setting it.
I am getting question mark symbol(?) instead of multiple white spaces in output excel. I am using apache poi 3.7. For single space it is working fine.
For example:-
if my input is "a b" then generated output is "a? b".
Here a and b have two spaces in between.
This code snippet works just fine.
Can you compare with your own code and post some code sample if you still have the problem ?
Workbook book = new HSSFWorkbook();
Sheet sheet = book.createSheet();
Row oRow = sheet.createRow(1);
Cell oCell = oRow.createCell(1);
oCell.setCellValue("a b");
OutputStream out = new FileOutputStream("c:\\temp\\test.xls");
book.write(out);
out.close();
Try to open your generated spreadsheet output in Microsoft Excel.
It is encoding issue. Sometimes it might happen that if your input contains multiple white spaces then Open office shows you as "?".
For future reference, this solved my problem. As Eric pointed, one should find out first which character codes are creating trouble, in my particular case they where zeroes.
String s = getStringFromSource();
s = s.replace('\u0000', '\u0020'); // check values with dec to hexa first, u0020 means 32
cell.setValue(s);
I have a csv file in which I am able to insert the header for the first run, but when I again write the file the program is creating the header again. Is there a way to check if csv file has a header and if yes then to skip it?
You would have to read the first line and test if the first column matches the column header you expect. Since your code inserts the header, I'm assuming it knows what the header should look like. You can use this same variable in your header check. Something like:
String HEADER = "column1,column2,column3";
String COLUMN1 = HEADER.substring(0,HEADER.indexOf(",")+1); //Or just set it to "column1", but that would be violating the DRY principle!
//...Get line1, column1 from the file you are reading
if(!line1Column1.equals(COLUMN1))
{
out.write(HEADER);
}
// Print rows of data...
Are you using any framework to do that or you are doing it yourself.. A code snippet would help... or you can put a Boolean flag to check or hard match the first line with the standard header code to check it...
If you inserted the header, couldn't you make it start, for instance with a dash (#) and if present not to write again ?
Regards,
Stéphane
Are you simply appending the records to the existing file and in which case the program is appending the header after the prior write?
Can you simply check if the file exists and if it does and is not size zero, assume the header is already present?