需求描述:有个同事想导出一份数据库中数据;由于数据的存储格式和同事需要的有部分差距,需要代码处理,处理完成后将数据导出成excel;代码如下:
第一步,首先先将mysql里面的数据根据需求以json导出;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import java.util.*;
@Data
@Slf4j
public class ExportRawExcelService {
ExcelWriter bigWriter;
int y = 1;
CellStyle linkStyle;
CellStyle celStyle;
public void prepare() {
bigWriter = ExcelUtil.getBigWriter();
bigWriter.setColumnWidth(0,20);
bigWriter.setColumnWidth(1,50);
bigWriter.setColumnWidth(2,50);
bigWriter.setDefaultRowHeight(10);
linkStyle = new ExcelStyleContext(bigWriter.getWorkbook()).createStyle("ORANGE", null, true, null);
celStyle = new ExcelStyleContext(bigWriter.getWorkbook()).createStyle("ORANGE", null, false, null);
String[] heads = {"订单编号", "码信息", "用酒名称", "状态"};
CellStyle headCellStyle = bigWriter.getHeadCellStyle();
for (int i = 0; i < heads.length; i++) {
Cell cell = bigWriter.getOrCreateCell(i, 0);
cell.setCellStyle(headCellStyle);
cell.setCellValue(heads[i]);
}
}
public void writerData(List<T.Temp> tempList) {
bigWriter.setSheet(0);
for (int i = 0; i < tempList.size(); i++) {
T.Temp temp = tempList.get(i);
int x = 0;
bigWriter.getOrCreateCell(x++, y).setCellValue(temp.getOrderCode());
bigWriter.getOrCreateCell(x++, y).setCellValue(temp.getCode());
bigWriter.getOrCreateCell(x++, y).setCellValue(temp.getWineName());
bigWriter.getOrCreateCell(x++, y).setCellValue(temp.getStatus());
y++;
}
}
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import org.apache.poi.ss.usermodel.Workbook;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class T {
public static void main(String[] args) throws IOException {
String path = "/temp/test.json";
T t = new T();
String strAll = getStrAll(new File(path));
JSONObject jsonObject = JSONObject.parseObject(strAll);
JSONArray records = jsonObject.getJSONArray("RECORDS");
List<JSONObject> jsonObjects = records.toJavaList(JSONObject.class);
ExportRawExcelService exportRawExcelService = new ExportRawExcelService();
exportRawExcelService.prepare();
exportRawExcelService.writerData(t.doWith(jsonObjects));
Workbook workbook = exportRawExcelService.getBigWriter().getWorkbook();
saveFile(workbook,"/temp/test.xlsx");
}
private List<Temp> doWith(List<JSONObject> strs){
List<Temp> tempList = new ArrayList<>();
for(int i = 0;i<strs.size();i++){
String sp = "",taskObjNo = "";
JSONObject data = strs.get(i);
for (String key : data.keySet()){
if (key.equals("name")){
taskObjNo = data.getString(key);
}else{
sp = data.getString(key);
}
}
String[] split1 = StrUtil.split(sp, "#$$#");
for (String s:split1){
Temp temp = new Temp();
temp.setOrderCode(taskObjNo);
JSONObject jsonObject = JSONObject.parseObject(s);
String code = jsonObject.getString("code");
JSONObject result = jsonObject.getJSONObject("result");
JSONObject data1 = result.getJSONObject("data");
if (data1 != null){
JSONObject signResult = data1.getJSONObject("识别结果");
if (signResult != null){
String hxStatus = signResult.getString("核销状态");
if (hxStatus == null){
temp.setStatus(result.getString("msg"));
}else{
temp.setStatus(hxStatus);
}
temp.setWineName(CommonUtil.get(() -> signResult.getString("产品名称"), ""));
}
}else{
temp.setStatus(result.getString("msg"));
}
if (temp.getStatus() == null){
temp.setStatus(jsonObject.getString("status"));
}
temp.setCode(code);
tempList.add(temp);
}
}
for (Temp temp : tempList){
System.out.println(temp.getOrderCode()+" "+temp.getCode()+" "+temp.getWineName()+" "+temp.getStatus());
}
return tempList;
}
@Data
public class Temp{
String orderCode;
String code;
String wineName;
String status;
}
public static String getStrAll(File jsonFile){
StringBuffer sb = new StringBuffer();
try {
InputStream is = new FileInputStream(jsonFile);
BufferedReader buffer = new BufferedReader(new InputStreamReader(is));
String ch = null;
while ((ch = buffer.readLine()) != null) {
sb.append(ch);
}
buffer.close();
is.close();
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
//把一个文件中的内容读取成一个String字符串
public static List<String> getStr(File jsonFile){
List<String> listStr = new ArrayList<>();
try {
InputStream is = new FileInputStream(jsonFile);
BufferedReader buffer = new BufferedReader(new InputStreamReader(is));
String ch = null;
while ((ch = buffer.readLine()) != null) {
listStr.add(ch);
}
buffer.close();
is.close();
return listStr;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
private static String saveFile(Workbook workbook, String filename) throws IOException {
String filepath = filename;
BufferedOutputStream out = FileUtil.getOutputStream(filepath);
workbook.write(out);
workbook.close();
IoUtil.close(out);
return filepath;
}