I was told to use hashmap in this code to allow a XLSX file to be uploaded and be able to be read on the website. The data I key into the XLSX has to be captured on the website too. As for now, there's a null exception error.
This is how my code looks like now.
OPCPackage pkg = null;
XSSFWorkbook workbook = null;
XSSFSheet sheet = null;
try {
pkg = OPCPackage.open("C:\\Users\\....Load_AcctCntr_Template.xlsx");
System.out.println(_LOC + "1.0 " + " pkg:" + pkg);
workbook = new XSSFWorkbook(pkg);
System.out.println(_LOC + "1.0 " + " workbook:" + workbook);
sheet = workbook.getSheetAt(Sheetindex);
System.out.println(_LOC + "1.0 " + " sheet:" + sheet);
pkg.close();
} catch (Exception e) {
System.out.println(_LOC + "1.0 " + " Test:");
return _m;
}
System.out.println(_LOC + "1.0 " + " sheet.getRow(0):" + sheet.getRow(0));
int _totalc = sheet.getRow(0).getLastCellNum();
int _totalr = sheet.getLastRowNum();
// Header r=0
String[] st = new String[_totalc];
for (int c = 0; c < _totalc; c++) {
st[c] = sheet.getRow(0).getCell(0).getStringCellValue();
}
_m.put("HEADER", st);
System.out.println(_LOC + "1.0 " + " _m:" + _m);
// Data r=1 thereafter
List list = new ArrayList();
for (int r = 1; r < _totalr; r++) {
Object[] o = new Object[_totalc];
String strRow = null;
for (int c = 0; c < _totalc; c++) {
o[c] = sheet.getRow(r).getCell(c);
String cn = o[c].getClass().getName();
String strCell = null;
if (!isEmptyNull(strCell)) {
strRow = "record_available";
}
}
if ((o != null) && (o.length != 0)) {
list.add(o);
}
}
_m.put("DATA", list);
System.out.println(_LOC + "1.0 " + " _m:" + _m);
return _m;
}
return _m;
}
What is shown on my console:
SystemOut O [RedirectLogin: requiredRights]1.0/BelsizeWeb/faces/module/entity/en1102.xhtml
SystemOut O [RedirectLogin: requiredRights]1.0en1102.xhtml
SystemOut O [En1102: doEn1102_command_readfileAction]1.0
SystemOut O [PageCodeBase: getConstructJXLList]1.0 pkg:org.apache.poi.openxml4j.opc.ZipPackage@a6742ad
SystemOut O [PageCodeBase: getConstructJXLList]1.0 workbook:Name: /xl/workbook.xml - Content Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml
SystemOut O [PageCodeBase: getConstructJXLList]1.0 sheet:Name: /xl/worksheets/sheet1.xml - Content Type: application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml
SystemOut O [PageCodeBase: getConstructJXLList]1.0 sheet.getRow(0):<xml-fragment r="1" spans="1:7" x14ac:dyDescent="0.25" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" xmlns:main="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
Caused by: java.lang.NullPointerException
at pagecode.PageCodeBase.getConstructJXLList_xlsx(PageCodeBase.java:6527)
at pagecode.module.entity.En1102.doEn1102_command_readfileAction(En1102.java:166)
getCell,getRowsandgetColumnsare not implemented and don't return any meaningful values. Once implemented please do more investigation and describe what exactly doesn't work for you. Try to restrict your question to the specific problem rather than posting a large block of code which doesn't work for some reason. Also when using Java try to follow Java naming conventions: java.about.com/od/javasyntax/a/nameconventions.htm and use meaningful names for your variables (_s_cis almost as expressive as_w_t_f)getCell,getRowsandgetColumns. But I added it because I was told not to use jxl and when I change some codes to be non-jxl, red underline appeared ongetColumns,getRowsandgetCellsaying that "The methodgetRows(),getColumns,getCellis undefined for the type PageCodeBase." So I tried to get the "help"/"quick fix" given and that's how I ended up with those methods.PageCodeBaseand where are you getting the errors you describe? There is so much going on here that you haven't shown us - I don't think you'll ever get a reasonable answer to the question in this form. Also, nobody prefixes variable names with underscores any more; it just makes everything much harder to read.PageCodeBaseis the name of the java file. I'll try to type it clearly again (Really sorry as I am not good in coding and not good in explaining too). These are the codes for xls previously. So I changed the codes to make it work for xlsx. Previously, the line of code forint _totalc = getColumns();suppose to be like thisint _totalc = sheet.getColumns();but I took away thesheetas the error said "method getColumns() is undefined for XSSFSheet." So I am wondering how do I solve this error of allowing getColumn to be defined for XSSFSheet.e.printStackTrace();immediately after} catch (Exception e) {then run it again and paste the output into the question?