ExcelUtil.java
1 package pers.kangxu.datautils.utils; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 import java.io.InputStream; 8 import java.util.ArrayList; 9 import java.util.HashMap; 10 import java.util.Iterator; 11 import java.util.List; 12 import java.util.Map; 13 14 import org.apache.poi.hssf.usermodel.HSSFCellStyle; 15 import org.apache.poi.hssf.usermodel.HSSFFont; 16 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 17 import org.apache.poi.ss.usermodel.Cell; 18 import org.apache.poi.ss.usermodel.CellStyle; 19 import org.apache.poi.ss.usermodel.CellValue; 20 import org.apache.poi.ss.usermodel.Font; 21 import org.apache.poi.ss.usermodel.FormulaEvaluator; 22 import org.apache.poi.ss.usermodel.Row; 23 import org.apache.poi.ss.usermodel.Sheet; 24 import org.apache.poi.ss.usermodel.Workbook; 25 import org.apache.poi.ss.util.CellRangeAddress; 26 27 /** 28 * 29 * 30 * excel 工具 31 * 32 * @author kangxu 33 * 34 */ 35 public class ExcelUtil { 36 37 /** 38 * 导出 excel 39 * @param filePath 文件全路径 40 * @param sheetName sheet页名称 41 * @param sheetIndex 当前sheet下表 从0开始 42 * @param fileHeader 头部 43 * @param datas 内容 44 */ 45 public static void writeExcel(String filePath,String sheetName, 46 int sheetIndex, 47 String[] fileHeader, 48 List datas){ 49 // 创建工作簿 50 Workbook wb = new HSSFWorkbook(); 51 // 创建工作表 sheet 52 Sheet s = wb.createSheet(); 53 54 wb.setSheetName(sheetIndex, sheetName); 55 56 Row r = s.createRow(0); 57 Cell c = null; 58 Font font = null; 59 CellStyle styleHeader = null; 60 CellStyle styleContent = null; 61 62 63 //粗体 64 font = wb.createFont(); 65 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); 66 // 设置头样式 67 styleHeader = wb.createCellStyle(); 68 styleHeader.setFont(font); 69 styleHeader.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框 70 styleHeader.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框 71 styleHeader.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框 72 styleHeader.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框 73 // 设置内容样式 74 styleContent = wb.createCellStyle(); 75 styleContent.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框 76 styleContent.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框 77 styleContent.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框 78 styleContent.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框 79 80 81 //设置头 82 for(int i=0;i > readExcel(String filePath,int sheetIndex){133 List
调用方式如下
ExcelUtilTester.java
1 package pers.kangxu.datautils.test; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import pers.kangxu.datautils.utils.ExcelUtil; 7 8 public class ExcelUtilTester { 9 10 public static void main(String[] args) {11 List datas = new ArrayList ();12 datas.add(new String[]{"狗熊","母","250"});13 datas.add(new String[]{"猪粮","不明","251"});14 //ExcelUtil.writeExcel("C:\\Users\\Administrator\\Desktop\\test\\test\\test.xls","sheet1",0, new String[]{"姓名","年龄","性别"}, datas);15 16 System.out.println(ExcelUtil.readExcel("C:\\Users\\Administrator\\Desktop\\test\\test\\test.xls", 0));17 18 }19 }