java区分不同的excel_Java处理excel两种不同的方式
1、背景說明
業(yè)務中有時候會碰到利用java 處理excel文件(生成、下載、上傳),通常生成一個excel文件會把它寫入到機器的一個臨時路徑,但有時候完全沒必要把它存下來,只需要在內(nèi)存中把這個文件轉(zhuǎn)為輸入流,至于后面怎么處理都行。
2、所需依賴包
Java中創(chuàng)建excel文件利用到了以下兩個依賴包poi-3.14.jar、poi-ooxml-3.14.jar:
org.apache.poi
poi
3.14
org.apache.poi
poi-ooxml
3.14
3、excel文件處理方式
3.1、方式一:保存為本地文件
通常在創(chuàng)建XSSFWorkbook后,可以通過以下這種方式來存儲該表格,即利用FileOutputStream把文件寫入其中,這個FileOutputStream指定了文件系統(tǒng)存在的路徑:
XSSFWorkbook wb = new XSSFWorkbook(); //創(chuàng)建工作薄
Sheet sheet = wb.createSheet("Sheet0"); //創(chuàng)建工作表,名稱為test
// 獲取文件路徑
filePath = "d://"+filename+"tongji.xls";
//文件輸出
try {
FileOutputStream out = new FileOutputStream(filepath);
// FileOutputStream out = new FileOutputStream("d://"+filename+"tongji.xls");
wb.write(out);
out.close();
result_return = true;
logger.info("ExcelService createExcelTable Result: "+ result_return);
} catch (IOException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
result_return = false;
logger.error("ExcelService createExcelTable Error: "+ e);
}
3.2、方式二:保存為輸入流
將XSSFWorkbook對象保存為輸入流時,利用到了ByteArrayOutputStream來做緩存,先將文件寫入其中,然后將其轉(zhuǎn)為字節(jié)數(shù)組,最后利用ByteArrayInputStream轉(zhuǎn)為輸入流,供后續(xù)使用。
XSSFWorkbook wb = new XSSFWorkbook(); //創(chuàng)建工作薄
Sheet sheet = wb.createSheet("Sheet0"); //創(chuàng)建工作表,名稱為test
ByteArrayInputStream in = null;
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
wb.write(os);
byte[] b = os.toByteArray();
in = new ByteArrayInputStream(b);
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
logger.error("ExcelUtils getExcelFile error:{}",e.toString());
return null;
}
return in;
}
3.3、FileOutputStream與ByteArrayOutputStream之間的區(qū)別
要了解FileOutputStream與ByteArrayOutputStream之間的區(qū)別,需要查閱java API。
先來看看API中對FileOutputStream的描述:
FileOutputStream需要指定具體文件或者文件描述路徑,才能完成數(shù)據(jù)寫入,可以把它理解為一個管道,管道不能存數(shù)據(jù)。
再來看看API中對ByteArrayOutputStream的描述:
ByteArrayOutputStream定義了一個ByteArray的輸出流,能夠往里面寫入數(shù)據(jù),其實就是內(nèi)存中的一個對象,同時實現(xiàn)了OutputStream。可以把它理解為一個自帶管道的容器,容器能直接存數(shù)據(jù)。
4、上傳OSS
接收到了excel文件的輸入流后,利用OSS接口實現(xiàn)上傳。
public static String uploadOSS(ByteArrayInputStream input,String issuenumber){
String url = null;
if(input != null){
String fileName = issuenumber + "_工單詳情.xlsx";
try {
HttpPost httpPost = new HttpPost("http://127.0.0.1/api/attachment/oss");
httpPost.addHeader("key","12345");
httpPost.addHeader("user","xiaomen");
httpPost.addHeader("method","FeedbackSelect");
httpPost.addHeader("filename",new String(fileName.getBytes("UTF-8"),"ISO-8859-1"));
httpPost.addHeader("type","01");
InputStreamEntity reqEntity = new InputStreamEntity(input);
httpPost.setEntity(reqEntity);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
if(responseEntity != null){
InputStream inputStream = responseEntity.getContent();
String data = convertStreamToString(inputStream);
logger.info("FileUpload2OSS uploadOSS data:{}",data);
JSONObject json = JSON.parseObject(data);
if(json.containsKey("statusCode")){
int statusCode = json.getIntValue("statusCode");
if(statusCode == 0){
JSONArray array = json.getJSONArray("responseData");
JSONObject j = array.getJSONObject(0);
url = j.getString("attachment");
}
}
}
input.close();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
logger.error("FileUpload2OSS uploadOSS UnsupportedEncodingException:{}",e.toString());
return url;
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
logger.error("FileUpload2OSS uploadOSS ClientProtocolException:{}",e.toString());
return url;
} catch (IOException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
logger.error("FileUpload2OSS uploadOSS IOException:{}",e.toString());
return url;
}
}
return url;
}
5、總結(jié)
針對具體業(yè)務可以自由選擇處理方式。如果無需存儲文件至系統(tǒng),則考慮第二種方式。
在我自己的業(yè)務中,使用了第二種方式,無需將生成的excel存在服務器即可完成上傳線上OSS。
總結(jié)
以上是生活随笔為你收集整理的java区分不同的excel_Java处理excel两种不同的方式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java util下的并发包_jdk并发
- 下一篇: java美元兑换,(Java实现) 美元