把對象轉爲字符串導出爲json文件
根據前段傳值獲取數據對象,轉成String,然後就可以在瀏覽器上面導出json或者TXT文件,備導入數據使用。
public class OpExportFileUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(OpExportFileUtil.class);
private HttpServletResponse response;
private HttpServletRequest request;
// 文件名
private String fileName = "export";
//文件保存路徑
private String fileDir;
//sheet名
private String sheetName = "sheet1";
//備註內容
private String titleRemark;
//必填項設置列表
private List<Integer> mandatoryTitle;
//表頭字體
private String titleFontType = "Arial Unicode MS";
//表頭字號
private short titleFontSize = 12;
//正文字體
private String contentFontType = "Arial Unicode MS";
//正文字號
private short contentFontSize = 12;
//首行備註字體
private String titleRemarkFontType = "宋體";
//首行備註字號
private short titleRemarkFontSize = 18;
//首行備註高度
private short titleRemarkCellHeight = 18;
//首行備註寬度(列號,從0起)
private short titleRemarkEndColNum = 12;
//是否需要序號列
private boolean needSequenceColumn = true;
//是否需要首行備註
private boolean needTitleRemark = false;
//是否需要標註必填項
private boolean needMandatoryTitle = false;
//說明的背景顏色
private Short titleRemarkBackGroundColorIndex = null;
//標題的北京顏色
private Short headerBackGroundColorIndex = null;
private XSSFWorkbook workbook = null;
// 導出的格式均爲文本 added by wentian 2018/4/9
private boolean isCellStylePureString = false;
//模板中是否需要下拉框,只適用於數量較小的情況
private boolean isNeedDropDownBox = false;
private int maxDropDownRowShowSize = 50000; //能展示下拉框的最大行數
private List<String[]> dropDownDatas; //下拉框的數據
private int[] dropDownColNums; //下拉框對應的列下標
private boolean titleRemarkBold = true;
public OpExportFileUtil setIsCellStylePureString(boolean flag) {
this.isCellStylePureString = flag;
return this;
}
public OpExportFileUtil(String fileDir, String sheetName) {
this.fileDir = fileDir;
this.sheetName = sheetName;
workbook = new XSSFWorkbook();
}
public OpExportFileUtil(HttpServletResponse response, String fileName) {
this.response = response;
this.fileName = fileName;
}
/**
* 設置表頭字體.
*
* @param titleFontType
*/
public OpExportFileUtil setTitleFontType(String titleFontType) {
this.titleFontType = titleFontType;
return this;
}
public OpExportFileUtil setTitleRemarkColorIndex(Short index) {
this.titleRemarkBackGroundColorIndex = index;
return this;
}
public OpExportFileUtil setHeaderColorIndex(Short index) {
this.headerBackGroundColorIndex = index;
return this;
}
public OpExportFileUtil setNeedSequenceColumn(boolean needSequenceColumn) {
this.needSequenceColumn = needSequenceColumn;
return this;
}
public OpExportFileUtil setNeedTitleRemark(boolean needTitleRemark) {
this.needTitleRemark = needTitleRemark;
return this;
}
public OpExportFileUtil setNeedMandatoryTitle(boolean needMandatoryTitle) {
this.needMandatoryTitle = needMandatoryTitle;
return this;
}
public OpExportFileUtil setTitleRemarkBold(boolean titleRemarkBold) {
this.titleRemarkBold = titleRemarkBold;
return this;
}
/**
* 設置表頭字體大小.
*
* @param titleFontSize
*/
public OpExportFileUtil setTitleFontSize(short titleFontSize) {
this.titleFontSize = titleFontSize;
return this;
}
/**
* 設置正文字體.
*
* @param contentFontType
*/
public OpExportFileUtil setContentFontType(String contentFontType) {
this.contentFontType = contentFontType;
return this;
}
/**
* 設置正文字號.
*
* @param contentFontSize
*/
public OpExportFileUtil setContentFontSize(short contentFontSize) {
this.contentFontSize = contentFontSize;
return this;
}
/**
* 設置首行備註信息
*
*/
public OpExportFileUtil setTitleRemark(String titleRemark, short titleRemarkFontSize, short titleRemarkCellHeight) {
this.titleRemark = titleRemark;
this.titleRemarkFontSize = titleRemarkFontSize;
this.titleRemarkCellHeight = titleRemarkCellHeight;
return this;
}
/**
* 設置首行備註信息
*
*/
public OpExportFileUtil setTitleRemark(String titleRemark, short titleRemarkFontSize, short titleRemarkCellHeight, short remarkEndColNum) {
this.titleRemark = titleRemark;
this.titleRemarkFontSize = titleRemarkFontSize;
this.titleRemarkCellHeight = titleRemarkCellHeight;
this.titleRemarkEndColNum = remarkEndColNum;
return this;
}
public OpExportFileUtil setMandatoryTitle(List<Integer> mandatoryTitle) {
this.mandatoryTitle = mandatoryTitle;
return this;
}
public OpExportFileUtil setDropDownBoxData(List<String[]> dropDownDatas, int[] dropDownColNums) {
this.dropDownDatas = dropDownDatas;
this.dropDownColNums = dropDownColNums;
if (null != dropDownDatas && dropDownDatas.size() > 0 && null != dropDownColNums
&& dropDownColNums.length > 0) {
this.isNeedDropDownBox = true;
}
return this;
}
/**
* 數據導出爲json文件
* @param exportContentStr
*/
public void writeOpExportFileJson(String exportContentStr) {
try (OutputStream out = getOutputStream();) {
// ByteArrayOutputStream excelStream = buildExcel(propertyNames, titleName, false, columnSizes, dataList);
out.write(exportContentStr.getBytes());
out.flush();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private OutputStream getOutputStream() throws IOException {
if (fileDir != null) {
// 保存文件
return new FileOutputStream(fileDir);
} else {
// 從response中獲取輸出流
buildResponse();
return response.getOutputStream();
}
}
private void buildResponse() throws UnsupportedEncodingException {
// 設置文件返回類型
fileName = fileName + ".json";
response.setContentType("application/msexcel");
// 默認處理
response.setHeader("Content-Disposition",
"attachment; filename=" + URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20"));
if (null == request) {
return;
}
String Agent = request.getHeader("User-Agent");
if (null != Agent && (Agent.toLowerCase().indexOf("firefox") != -1)) {
//對火狐做特殊處理
response.setHeader("Content-Disposition",
String.format("attachment;filename*=utf-8'zh_cn'%s", URLEncoder.encode(fileName, "utf-8")));
}
}
public void writeTXT(String path, String title, String content, HttpServletResponse response) {
try {
// 防止文件建立或讀取失敗,用catch捕捉錯誤並打印,也可以throw
/* 寫入Txt文件 */
File writename = new File(path);// 相對路徑,如果沒有則要建立一個新的output。txt文件
if (!writename.exists()) {
writename.mkdirs();
}
writename = new File(path + "\\" + title);// 相對路徑,如果沒有則要建立一個新的output。txt文件
writename.createNewFile(); // 創建新文件
BufferedWriter out = new BufferedWriter(new FileWriter(writename));
out.write(content); // \r\n即爲換行
out.flush(); // 把緩存區內容壓入文件
out.close(); // 最後記得關閉文件
} catch (Exception e) {
e.printStackTrace();
}
}
}
每天努力一點,每天都在進步。