I. background introduction
The Java Web environment needs to read the data in the excel table and insert it into the database. The xssfworkbook class needs to be used to read the table file. The key codes are as follows:
//read excel
public static Workbook readExcel(String filePath) {
Workbook wb = null;
if (filePath == null) {
return null;
}
String extString = filePath.substring(filePath.lastIndexOf("."));
InputStream is = null;
try {
is = new FileInputStream(filePath);
if (".xls".equals(extString)) {
return wb = new HSSFWorkbook(is);
} else if (".xlsx".equals(extString)) {
return wb = new XSSFWorkbook(is);
} else {
return wb = null;
}
} catch (FileNotFoundException e) {
LOGGER.error(e.toString());
} catch (IOException e) {
LOGGER.error(e.toString());
}
return wb;
}
II. Occurrence of problems
Running locally, such as writing a main method or running with @ test, can run normally, but on the web, the interruption point finds that this class cannot be found
so it tries to upgrade the dependency package
or not. Unable to solve it, he fell into deep thinking. It’s really hard to deal with environmental problems and dependency problems
tried the recommended dependencies and versions given by other authors, but still failed:
First:
Second:
Three problem solving
Bypass the problem. If the mountain doesn’t come, I’ll go
change the file format: that is, xlsx is XLS, and finally return to the hssfworkbook class for reading.
PATH = "/Users/xxx/work/B/A_info.xlsx";
by
PATH = "/Users/xxx/work/B/A_info.xls";
So you can access it in the web layer
(PS: it should be noted that if you directly change the. Xlsx format file to. XLS, you need to open the file first and click Save as. XLS format. You can’t be lazy here. The author has also stepped on this pit)
Read More:
- Django WSGI protocol application, based on wsgiref module DIY a web framework
- Extracting Data from XML (Using Python to Access Web Data)
- [Solved] AttributeError WriteOnlyWorksheet object has no attribute cell
- python2.7 ExcelWriter error Exception caught in workbook destructor. Explicit close() may be require
- Python openpyxl excel open zipfile error resolution: zipfile.BadZipFile: File is not a zip file
- [Solved] raise KeyError(key) from err KeyError: ‘Dates‘
- The automatic token of Python interface is passed into the header
- [Solved] ValueError: row index was 65536, not allowed by .xls format
- How to Fix pandas.errors.ParserError Error tokenizing data C error Buffer overflow caught
- [Solved] Cannot open .xlsx file, xlrd.biffh.XLRDError: Excel xlsx file; not supported
- Pyexcel Error: xlrd.biffh.XLRDError: Excel xlsx file; not supported
- Python Error: SyntaxError: (unicode error) ‘unicodeescape‘ codec can‘t decode bytes in position 2-3:
- Python reads excel and stores it in the list according to the header hash copy.deepcopy
- How to Solve Python Xlwt ValueError: More than 4094 XFs (styles)
- [Solved] AttributeError: ‘DataFrame‘ object has no attribute ‘tolist‘
- NameError: name ‘null’ is not defined [How to Solve]
- [Solved] pandas ExcelWrite AttributeError: ‘NoneType‘ object has no attribute ‘group‘
- Python: How to get the size of the picture (byte/kb/mb)
- How to Solve wikiextractor Extract Wikipedia Corpus Error