我使用端點來上傳.xlsx或.xls檔案。之后,我將編碼的 excel 檔案(base64)存盤到字串中。為了決議和處理 excel 的值,我需要解碼該檔案。
上傳服務類:
public BulkUploadResponse validateFile(BulkUploadRequest request) {
final String pfx = String.format("validateFile: ");
BulkUploadResponse response = new BulkUploadResponse();
String delimiters = "\\s |,";
String[] tokensVal = request.getContent().split(delimiters);
String fileContentEncoded = tokensVal[tokensVal.length-1];
InputStream fileContent = new ByteArrayInputStream(new String(Base64.getDecoder().decode(fileContentEncoded), UTF_8).getBytes(UTF_8));
然后我呼叫一個類,在那里我將包含在 excel 檔案決議中的資料轉換fileContent為應該被解碼的引數。
BulkVignetteCustomer customerVignettes = bulkVignetteCustomerConverter.convert(fileContent);
最后我使用這個Workbook包來決議那個檔案
例子:
@Override
public BulkVignetteCustomer convert(InputStream fileContent) {
try {
Workbook wb = WorkbookFactory.create(fileContent);
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
Sheet sheet = wb.getSheetAt(0);
Iterator rows = sheet.iterator();
(...)
我得到的錯誤:
java.lang.IllegalArgumentException: Your InputStream was neither an OLE2 stream, nor an OOXML stream at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:89)
關于如何解碼檔案而沒有任何錯誤的任何想法?
謝謝!
uj5u.com熱心網友回復:
這條線對我來說似乎很奇怪:
InputStream fileContent = new ByteArrayInputStream(new String(Base64.getDecoder().decode(fileContentEncoded), UTF_8).getBytes(UTF_8));
在此階段,您有一個 Excel 檔案,它不是純文本,而是一個大的二進制 blob。為什么在這里使用字串?
所以二進制 blob 被編碼為 Base64。您可以直接從解碼器中取出位元組并將它們放入 ByteArrayInputStream 中:
InputStream fileContent = new ByteArrayInputStream(Base64.getDecoder().decode(fileContentEncoded));
要驗證應用程式的這一部分,您可以將位元組存盤到檔案中并嘗試使用 Excel 打開它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/354005.html
上一篇:如果列沒有任何大于1的值,則退出
