1、用到的jar包:fastjson-1.2.9、poi(poi-3.15、poi-ooxml-3.15、poi-ooxml-schemas-3.15、xmlbeans-2.6.0、commons-collections4-4.1)
很简单,直接上代码:
2、导出类,两个WrapAll类字符串数组都是excel文件名,如item.xlsx,写死的读取sheet 第 0 页
ParseJson方法导出为json,list是行,Map key-value:字段名-值
1 package com.ojcgame.warp; 2 3 import java.io.FileInputStream; 4 import java.io.FileWriter; 5 import java.io.IOException; 6 import java.io.InputStream; 7 import java.lang.reflect.InvocationTargetException; 8 import java.util.ArrayList; 9 import java.util.HashMap; 10 import java.util.List; 11 import java.util.Map; 12 13 import org.apache.poi.hssf.usermodel.HSSFCell; 14 import org.apache.poi.xssf.usermodel.XSSFCell; 15 import org.apache.poi.xssf.usermodel.XSSFRow; 16 import org.apache.poi.xssf.usermodel.XSSFSheet; 17 import org.apache.poi.xssf.usermodel.XSSFWorkbook; 18 import org.eclipse.core.runtime.IProgressMonitor; 19 import org.eclipse.jface.dialogs.ProgressMonitorDialog; 20 import org.eclipse.jface.operation.IRunnableWithProgress; 21 22 import com.alibaba.fastjson.JSON; 23 import com.ojcgame.common.EnvironmentManager; 24 import com.ojcgame.common.OJCUtils; 25 26 public class WarpDataManager { 27 String[] filesArr; 28 29 public void WarpAll(String[] files) { 30 filesArr = files; 31 ProgressMonitorDialog progress = new ProgressMonitorDialog(null); 32 IRunnableWithProgress progressTask = new IRunnableWithProgress() { 33 @Override 34 public void run(IProgressMonitor monitor) 35 throws InvocationTargetException, InterruptedException { 36 monitor.beginTask("正在导出数据", IProgressMonitor.UNKNOWN); 37 WarpAll(filesArr, monitor); 38 } 39 }; 40 41 try { 42 progress.run(true, false, progressTask); 43 } catch (InvocationTargetException e) { 44 e.printStackTrace(); 45 } catch (InterruptedException e) { 46 e.printStackTrace(); 47 } finally { 48 filesArr = null; 49 } 50 } 51 52 @SuppressWarnings("deprecation") 53 private void WarpAll(String[] files, IProgressMonitor monitor) { 54 InputStream is = null; 55 XSSFWorkbook xssfWorkbook = null; 56 List titles = null; 57 Map oneCellData = null; 58 List