Android poi工具类 只关注数据,抽象出写入细节
使用poi excel类库
添加依赖 //poi excel implementation ‘org.apache.poi:poi-scratchpad:3.15-beta2’
ExcelUtil
import org.apache.poi.hssf.usermodel.HSSFBorderFormatting;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class ReortFormUtil {
private ExcelUtil() {
}
private static ExcelUtil instance = new ExcelUtil();
public static ExcelUtil getInstance() {
return instance;
}
public void makeExcel(List<String> heads, Map<Integer, Map<Integer, String>> logs, String path,String fileName) {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
createExcelHead(wb, sheet, heads);
createBody(wb, sheet, heads.size(), logs);
createFile(wb,path,fileName);
}
private void createExcelHead(HSSFWorkbook wb, HSSFSheet sheet, List<String> heads) {
HSSFRow row = sheet.createRow(0);
HSSFCellStyle titleStyle = wb.createCellStyle();
HSSFFont titleFont = wb.createFont();
titleFont.setFontHeightInPoints((short) 12);
titleStyle.setFont(titleFont);
titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
titleStyle.setFillForegroundColor(HSSFColor.GOLD.index);
titleStyle.setBorderBottom(HSSFBorderFormatting.BORDER_THIN);
titleStyle.setBorderTop(HSSFBorderFormatting.BORDER_THIN);
titleStyle.setBorderLeft(HSSFBorderFormatting.BORDER_THIN);
titleStyle.setBorderRight(HSSFBorderFormatting.BORDER_THIN);
for (int i = 0; i < heads.size(); i++) {
HSSFCell cell = row.createCell(i);
cell.setCellStyle(titleStyle);
cell.setCellValue(heads.get(i));
}
}
private void createBody(HSSFWorkbook wb, HSSFSheet sheet, int titleSize, Map<Integer, Map<Integer, String>> logs) {
HSSFCellStyle bodyStyle = wb.createCellStyle();
HSSFFont bodyFont = wb.createFont();
bodyFont.setFontHeightInPoints((short) 10);
bodyStyle.setFont(bodyFont);
bodyStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
bodyStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
bodyStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
bodyStyle.setBorderBottom(HSSFBorderFormatting.BORDER_THIN);
bodyStyle.setBorderTop(HSSFBorderFormatting.BORDER_THIN);
bodyStyle.setBorderLeft(HSSFBorderFormatting.BORDER_THIN);
bodyStyle.setBorderRight(HSSFBorderFormatting.BORDER_THIN);
for (int i = 0; i < logs.size(); i++) {
HSSFRow hssfRow = sheet.createRow(i+1);
Map<Integer, String> maps = logs.get(i);
Set<Integer> cells = maps.keySet();
for (Integer key : cells) {
HSSFCell cell = hssfRow.createCell(key);
String data = maps.get(key);
cell.setCellStyle(bodyStyle);
cell.setCellValue(data);
sheet.setColumnWidth(key,(data.getBytes().length+8)* 256 );
}
}
}
private void createFile(HSSFWorkbook wb, String path,String fileName) {
FileOutputStream output = null;
File file = new File(path);
if (!file.exists()) {
System.out.println("createNewExcel: 文件夹不存在,开始创建");
file.mkdir();
} else {
System.out.println("createNewExcel: 文件夹已存在");
}
try {
output = new FileOutputStream(path+"/"+fileName);
wb.write(output);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
output.flush();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
注意事项 1:文件读写权限 2:标题中的list数据顺序与body的map相互对应 3:poi库各版本间api存在差异,如修改依赖库版本,会出现不兼容问题
|