I am building an itext report with nested PdfPTables. I have been able to verify that the data is getting from the database to my data objects, but I have not been able to get that data to show up in the body of the report. The Header, which I had directly copied from a previous (functioning) report is able to add the rows, but all I get for my trouble in the body is a single line (not a single line of data, a single line as if drawn on the report itself).
I am hopeful that I am merely forgetting something simple in the middle. I will show a few of my methods:
public void makePDF(String jsonData, Document document, PdfWriter writer)
{
this.beginNewLandscapePage(document, writer);
try
{
jsonData = DBConnect.PrintReport(CommonConstants.reportAPI,this.getParams());
JSONObject json = (JSONObject) getParser().parse(jsonData);
//System.out.println("json = "+json);
JSONObject reportHeaderObject = (JSONObject) (json.get(CommonConstants.reportHeaderObjectField));
JSONObject reportPayloadObject = (JSONObject) (json.get(CommonConstants.reportPayloadObjectField));
JSONArray reportData = (JSONArray) (reportPayloadObject.get(CommonConstants.reportDataArrayField));
JSONObject reportTotals = (JSONObject) (reportPayloadObject.get(CommonConstants.reportTotalsObjectField));
PdfPTable mainTable = new PdfPTable(1);
mainTable.setWidthPercentage(100);
mainTable.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
this.buildHeader(reportHeaderObject,writer);
boolean showSubTotalLabel;
try
{
showSubTotalLabel = this.getIsSubTotaled();
}//try
catch (NullPointerException ne)
{
showSubTotalLabel = false;
}//catch (NullPointerException)
String sortStub = null;
if(showSubTotalLabel)
{
sortStub = Utils.getJSONAsString(reportPayloadObject, CommonConstants.sortStubField);
}//if(showSubTotalLabel)
for (int i = 0; i < reportData.size(); i++)
{
JSONObject row = (JSONObject) reportData.get(i);
mainTable.addCell(this.buildRow(row,showSubTotalLabel,sortStub,false));
}//for (int i = 0; i < dedInfo.size(); i++)
mainTable.addCell(this.buildRow(reportTotals,showSubTotalLabel,sortStub,true));
document.add(mainTable);
}//try
catch (ParseException e)
{
e.printStackTrace();
}//catch (ParseException)
catch (DocumentException e)
{
e.printStackTrace();
}//catch (DocumentException)
}//makePDF(String, Document, PdfWriter)
public PdfPCell buildRow(JSONObject row,boolean showSubTotalLabel,String sortStub,boolean isGrandTotal)
{
PdfPTable innerRowTable = new PdfPTable(this.getInnerRowTableWidths());
PdfPTable outerRowTable = new PdfPTable(this.getOuterRowTableWidths());
PdfPTable resultTable = new PdfPTable(1);
PdfPTable dedColumn;
PdfPTable leftColumn;
PdfPTable payColumn;
PdfPTable rightColumn;
PdfPTable taxColumn;
PdfPCell result;
PayrollRegisterData rowData2;
int numRows = 5; //Number of rows in the right column, minus the two matched by the totals row and header row
if(showSubTotalLabel)
{
resultTable.addCell(this.addSubTotalHeader(row,sortStub));
}//if(showSubTotalLabel)
boolean isTotal = true;
try
{
JSONObject totalObject = (JSONObject)row.get(CommonConstants.totalsObjectField); //test only to force the NullPointerException
}//try
catch(NullPointerException npe)
{
isTotal = false;
}//catch(NullPointerException)
if (isTotal)
{
PayrollRegisterVoucherData rowData = new PayrollRegisterVoucherData(row);
showSubTotalLabel = false;
rowData2 = rowData;
leftColumn = this.buildLeftColumnVoucher(rowData);
rightColumn = this.buildRightColumnVoucher(rowData);
}//if (isTotal)
else
{
PayrollRegisterTotalData rowData = new PayrollRegisterTotalData(row);
numRows = 4; //Number of Rows in the left column of the totals minus the two for the totals and header
showSubTotalLabel = true;
rowData2 = rowData;
leftColumn = this.buildLeftColumnTotal(rowData);
rightColumn = this.buildRightColumnTotal(rowData);
if(isGrandTotal)
{
resultTable.addCell(this.addTotalHeader(CommonConstants.reportTotalLabel));
}//if(isGrandTotal)
else
{
resultTable.addCell(this.addTotalHeader(row,sortStub));
}//else
}//else
if (rowData2.getDeductionData().length > numRows)
{
numRows = rowData2.getDeductionData().length;
}//if (rowData.getDeductionData().length > numRows)
if (rowData2.getPayData().length > numRows)
{
numRows = rowData2.getPayData().length;
}//if (rowData.getPayData().length > numRows)
if (rowData2.getTaxData().length > numRows)
{
numRows = rowData2.getTaxData().length;
}//if (rowData.getTaxData().length > numRows)
dedColumn = this.buildDedColumn(rowData2,numRows);
payColumn = this.buildPayColumn(rowData2,numRows);
taxColumn = this.buildTaxColumn(rowData2,numRows);
innerRowTable.addCell(Utils.getBlankCell());
innerRowTable.addCell(payColumn);
innerRowTable.addCell(Utils.getBlankCell());
innerRowTable.addCell(taxColumn);
innerRowTable.addCell(Utils.getBlankCell());
innerRowTable.addCell(dedColumn);
innerRowTable.addCell(Utils.getBlankCell());
outerRowTable.addCell(leftColumn);
outerRowTable.addCell(innerRowTable);
outerRowTable.addCell(rightColumn);
resultTable.addCell(outerRowTable);
result = new PdfPCell(resultTable);
return result;
}//buildRow(JSONObject,boolean,String,boolean)
I know I am a bit rusty with this, but the data is getting into the data objects successfully, and it is running without errors. It just doesn't show any data in the data section of the report. Is my problem with my mainTable, my buildRow, or do those look right and I need to look deeper into my code?
It turns out that the issue was not posted in the original code snippet, at least not completely. The issue was that the float[] variables I had set in the constructor were reversed for the inner and outer tables. Thus, the outer table, which I thought only wanted three columns per row, was expecting 7 columns. So, when I added it to the resultTable, it was not adding anything at all.
Related
I am new to Apache POI.
I have written a small code for removing duplicate records from a excel file. I am successfully able to identify the duplicate records across sheets but when writing to a new file after removing records, no output is being generated.
Please help where I am goin wrong?
Am I writing properly ?? Or am missing something?
public static void main(String args[]) {
DataFormatter formatter = new DataFormatter();
HSSFWorkbook input_workbook;
HSSFWorkbook workbook_Output_Final;
HSSFSheet input_workbook_sheet;
HSSFRow row_Output;
HSSFRow row_1_index;
HSSFRow row_2_index;
String value1 = "";
String value2 = "";
int count;
//main try catch block starts
try {
FileInputStream input_file = new FileInputStream("E:\\TEST\\Output.xls"); //reading from input file
input_workbook = new HSSFWorkbook(new POIFSFileSystem(input_file));
for (int sheetnum = 0; sheetnum < input_workbook.getNumberOfSheets(); sheetnum++) { //traversing sheets
input_workbook_sheet = input_workbook.getSheetAt(sheetnum);
int input_workbook_sheet_total_row = input_workbook_sheet.getLastRowNum(); //fetching last row nmber
for (int input_workbook_sheet_row_1 = 0; input_workbook_sheet_row_1 <= input_workbook_sheet_total_row; input_workbook_sheet_row_1++) { //traversing row 1
for (int input_workbook_sheet_row_2 = 0; input_workbook_sheet_row_2 <= input_workbook_sheet_total_row; input_workbook_sheet_row_2++) {
row_1_index = input_workbook_sheet.getRow(input_workbook_sheet_row_1); //fetching one iteration row index
row_2_index = input_workbook_sheet.getRow(input_workbook_sheet_row_2); //fetching sec iteration row index
if (row_1_index != row_2_index) {
count = 0;
value1 = "";
value2 = "";
for (int row_1_index_cell = 0; row_1_index_cell < row_1_index.getLastCellNum(); row_1_index_cell++) { //traversing cell for each row
try {
value1 = value1 + formatter.formatCellValue(row_1_index.getCell(row_1_index_cell)); //fetching row cells value
value2 = value2 + formatter.formatCellValue(row_2_index.getCell(row_1_index_cell)); //fetching row cells value
} catch (NullPointerException e) {
}
count++;
if (count == row_1_index.getLastCellNum()) {
if (value1.hashCode() == value2.hashCode()) { //remove the duplicate logic
System.out.println("deleted : " + row_2_index);
System.out.println("------------------");
input_workbook_sheet.removeRow(row_2_index);
}
}
}
}
}
}
}
FileOutputStream fileOut = new FileOutputStream("E:\\TEST\\workbook.xls");
input_workbook.write(fileOut);
fileOut.close();
input_file.close();
} catch (Exception e) {
//e.printStackTrace();
}
//main try catch block ends
}
A couple of things to note:
you swallow any kind of Exception; Igotsome nullpointers with my test data, and that would prevent the workbook from being written
when removing rows, it is an old trick to move backwards through the row numbers because then you don't have to adjust for the row number you have just removed
the code empties the row, but it doesn't move all rows upwards (=there is a gap after the delete). If you want to remove that gap, you can work with shiftRows
you compare things by hashcode, which is possible (in some use cases), but I feel like .equals() is what you want to do. See also Relationship between hashCode and equals method in Java
Here's some code that worked for my test data, feel free to comment if something doesn't work with your data:
public static void main(String args[]) throws IOException {
DataFormatter formatter = new DataFormatter();
HSSFWorkbook input_workbook;
HSSFWorkbook workbook_Output_Final;
HSSFSheet input_workbook_sheet;
HSSFRow row_Output;
HSSFRow row_1_index;
HSSFRow row_2_index;
String value1 = "";
String value2 = "";
int count;
FileInputStream input_file = new FileInputStream("c:\\temp\\test.xls");
input_workbook = new HSSFWorkbook(new POIFSFileSystem(input_file));
for (int sheetnum = 0; sheetnum < input_workbook.getNumberOfSheets(); sheetnum++) {
input_workbook_sheet = input_workbook.getSheetAt(sheetnum);
int input_workbook_sheet_total_row = input_workbook_sheet.getLastRowNum();
for (int input_workbook_sheet_row_1 = input_workbook_sheet_total_row; input_workbook_sheet_row_1 >=0; input_workbook_sheet_row_1--) { // traversing
for (int input_workbook_sheet_row_2 = input_workbook_sheet_total_row; input_workbook_sheet_row_2 >= 0 ; input_workbook_sheet_row_2--) {
row_1_index = input_workbook_sheet.getRow(input_workbook_sheet_row_1);
row_2_index = input_workbook_sheet.getRow(input_workbook_sheet_row_2);
if (row_1_index != null && row_2_index != null && row_1_index != row_2_index) {
count = 0;
value1 = "";
value2 = "";
int row_1_max = row_1_index.getLastCellNum() - 1;
for (int row_1_index_cell = 0; row_1_index_cell < row_1_max; row_1_index_cell++) {
try {
value1 = value1 + formatter.formatCellValue(row_1_index.getCell(row_1_index_cell));
value2 = value2 + formatter.formatCellValue(row_2_index.getCell(row_1_index_cell));
} catch (NullPointerException e) {
e.printStackTrace();
}
count++;
if (value1.equals(value2)) {
System.out.println("deleted : " + row_2_index.getRowNum());
System.out.println("------------------");
input_workbook_sheet.removeRow(row_2_index);
input_workbook_sheet.shiftRows(
row_2_index.getRowNum() + 1,
input_workbook_sheet_total_row,
-1,
true,
true);
}
}
}
}
}
}
FileOutputStream fileOut = new FileOutputStream("c:\\temp\\workbook.xls");
input_workbook.write(fileOut);
fileOut.close();
input_file.close();
input_workbook.close();
}
I have a java application that exports some data to a spreadsheet using POI. The problem is that sometimes (not always) there are a couple of lines that have no information (they are "blank") but when I save the XLS document to my desktop and open it, the cells are filled with the correct information.
Im using POI 3.7.
Down here you have an example of what's happening.
[EDIT] - I add some code:
This is the method that insert the data into the sheet:
public void setTransactionDetailsData(HSSFSheet sheet, String[][] records){
HSSFRow rowContent = null;
HSSFCell cellContent = null;
String cell = "";
Integer recordNumber = records.length;
Integer columnNumber = records[0].length;
Integer nextDataIndex = 0;
String styleText = "";
boolean customStyles = styles.size()>0;
short alignment;
CellStyle cellStyle = null;
for(int i = 0; i<recordNumber;i++){
//New row
rowContent = sheet.createRow(lastRowCreated);
rowContent.setHeightInPoints((customDataCellHeight * sheet.getDefaultRowHeightInPoints()));
for(int j = 0; j<columnNumber;j++){
cellContent = null;
cellContent = rowContent.createCell((nextDataIndex));
cell = records[i][j];
if(customStyles){
try{
styleText = styles.get(nextDataIndex);
alignment = getAlignment(styleText);
}catch(IndexOutOfBoundsException e){
LOG.info("Error while obtaining style for position " +nextDataIndex+ ", max index is " +(styles.size()-1)+ ". Check the list of styles you send");
styleText = "";
alignment = 1;
}
}else{
alignment = 1;
}
cellStyle = getCellStyle(i,Integer.valueOf(alignment),styleText);
//Text
if("".equals(styleText) || "value_left".equals(styleText) || "value".equalsIgnoreCase(styleText)){
cellContent.setCellValue(cell);
}else{
if(isNumeric(cell)){
try{
cellContent.setCellValue(convertoToParseableNumber(cell));
cellContent.setCellType(0);
}catch(NumberFormatException e){
LOG.info("Error ocurred while parsing value " +cell+ " no number");
cellContent.setCellValue(cell);
}
}else{
cellContent.setCellValue(cell);
}
}
cellContent.setCellStyle(cellStyle);
nextDataIndex++;
}
//Back to first column
nextDataIndex = 0;
lastRowCreated++;
}
autosize(sheet);
}
Thank you in advance.
I am trying to read Excel -2*2 matrix through Apache POI. But the first value returned by 2D array is [null,null]. Please check my code and advise for suitable corrections.
public String[][] getDataArray(String sheetName)
{
String value ="";
String[][] data = null;
int rowCount = wb.getSheet(sheetName).getLastRowNum();
int colCount = wb.getSheet(sheetName).getRow(1).getLastCellNum()-1;
data = new String[rowCount][colCount];
for(int i=1; i<=rowCount;i++)
{
Row row = wb.getSheet(sheetName).getRow(i);
for(int j=0;j<colCount;j++)
{
Cell cell = row.getCell(j);
if(cell.getCellType()==Cell.CELL_TYPE_NUMERIC)
{
value = ""+cell.getStringCellValue();
}
else
{
value = cell.getStringCellValue();
}
data[i][j] = value;
}
}
return data;
}
The debug view where we can see that the first value stored in the variable data is null, null
The excel which i am trying to read. I need only the userName and password data(2*2) alone. Not the header and Run mode datas.
Of course the value in the index 0 will be null because the i starts from 1 and not 0
for (int i = 1; i <= rowCount; i++) //i starts from one
...
data[i][j] = value;
either initialize the i from 0 or do like this
data[i-1][j] = value;
public static String[][] getSheetData(final String fileName, final String workSheetName)
throws Exception {
Integer lastRow = null;
short lastCol = 0;
String[][] sheetData = null;
FileInputStream file=new FileInputStream(MettlTest.class.getClass().getResource("/" + fileName).getPath());
workbook = new XSSFWorkbook(file);
sheet = workbook.getSheet(workSheetName);
try {
XSSFRow row;
XSSFCell cell;
lastRow = sheet.getPhysicalNumberOfRows();
lastCol = sheet.getRow(1).getLastCellNum();
sheetData = new String[lastRow - 1][lastCol];
for (int r = 1; r < lastRow; r++) {
row = sheet.getRow(r);
if (row != null) {
for (int c = 0; c < lastCol; c++) {
cell = row.getCell(c);
if (cell == null) {
sheetData[r][c] = null;
} else {
sheetData[r-1][c] = new DataFormatter().formatCellValue(cell);
}
}
}
}
return sheetData;
}
catch (final Exception e) {
throw e;
}
finally {
try {
file.close();
} catch (IOException io) {
Reporter.log("Unable to close File : " + fileName);
throw io;
}
}
I have default table model fetch with data from database and i want to print in doc word as a table. How to achieve that. See the code below:
try {
try {
con = connCC.getDBconnection();
} catch (ClassNotFoundException ex) {
Logger.getLogger(CustomerSR.class.getName()).log(Level.SEVERE, null, ex);
}
stm = con.createStatement();
ResultSet rs = stm.executeQuery("Select * From Appointment");
while (rs.next()) {
for (int col = 0; col < 1; col++) {
rowData.add(rs.getString(1));
rowData.add(rs.getString(2));
rowData.add(rs.getString(3));
rowData.add(rs.getString(4));
rowData.add(rs.getString(5));
}
model.addRow(rowData);
}
window.display(model);
//window.display(names, phones, addresses);
} catch (SQLException ex) {
ex.printStackTrace();
}
}
In the above comments, I see that you are already using Apache POI. If I understand correctly, you want to input data from a database into a table on a word document. Take a look at this tutorial on creating tables in word with Apache POI:
http://www.tutorialspoint.com/apache_poi_word/apache_poi_word_tables.htm
You can set the text directly with the data that you retrieve from the database. I can post an example if you need it, but the tutorial does a pretty good job of showing what you need to do.
----UPDATE----
Sorry that it took me a while, went to lunch with the wife. Try this:
// Blank Document
XWPFDocument document = new XWPFDocument();
// Write the Document in file system
FileOutputStream out = new FileOutputStream(new File("create_table.docx"));
// Create table
XWPFTable table = document.createTable();
// Table row
XWPFTableRow tableRow;
int rowCount = model.getRowCount() - 1;
int colCount = model.getColumnCount() - 1;
// Iterate through rows
for (int row = 0; row <= rowCount; row++) {
tableRow = table.getRow(row);
// Iterate through columns
for (int col = 0; col <= colCount; col++) {
tableRow.getCell(col).setText((String) model.getValueAt(row, col));
// If more columns, add cell
if (row == 0 && col < colCount) {
tableRow.addNewTableCell();
}
}
// If more rows, add row
if (row < rowCount) {
table.createRow();
}
}
// Write to word document and close file.
document.write(out);
out.close();
in my application I'm trying to extract the values from a filter list (auto-complete field).
In that field I have [ID, Name] ex [j342234, A,S]. I was able to retrieve the whole criteria by
doing this
Object Filterlistresult = fliterList.getCriteria();
but now I want to get extract ID part only from that field. Any Ideas?
Thank you in advance.
// here I get the values from a web server and insert them to the work Vector
public void parseJSONResponceInWB(String jsonInStrFormat) {
try {
JSONObject json = new JSONObject(jsonInStrFormat);
JSONArray jArray = json.getJSONArray("transport");
for (int i = 1; i < jArray.length(); i++) {
JSONObject j = jArray.getJSONObject(i);
ID = j.getString("ID");
Name = j.getString("Name");
WBenchVector.addElement(ID + " " + Name);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
// adding the values to the filter list
private void RequestAutoComplete() {
String[] returnValues = new String[WBenchVector.size()];
System.out.print(returnValues);
for (int i = 0; i < WBenchVector.size(); i++) {
returnValues[i] = (String) WBenchVector.elementAt(i);
}
autoCompleteField(returnValues);
}
// creating the filter list field
private void autoCompleteField(String[] returnValues) {
filterLst = new BasicFilteredList();
filterLst.addDataSet(ID, returnValues, "",
BasicFilteredList.COMPARISON_IGNORE_CASE);
autoFld.setFilteredList(filterLst);
}
Finally i'm getting what the user select using this
AutoFieldCriteria = filterLst.getCriteria();
Thank you Nate. I actually fixed the problem.
I converted the filterField Object to String and then I looked for the index of the first space, and substring the first word
Object AutoFieldCriteria = filterLst.getCriteria();
String AutoFieldString = AutoFieldCriteria.toString();
int spacePos = AutoFieldString.indexOf(" ");
String AutoFieldFirstWord = AutoFieldString.substring(0,spacePos);
Thanks again.