Excel导入、导出
其他
2018-12-20 07:40:51
阅读次数: 0
- package com.poi;
-
- import java.util.List;
-
- import org.apache.poi.hssf.usermodel.HSSFCellStyle;
-
-
-
-
-
-
-
-
-
-
-
-
-
- public class ExcelEntity {
- private String sheetName;
-
- private String[] columnNames;
-
- private String[] propertyNames;
-
- private String[] cLabels;
-
- private int rpp = 200;
-
- private HSSFCellStyle style = null;
-
- @SuppressWarnings("rawtypes")
- private List resultList;
-
- public String getSheetName() {
- return sheetName;
- }
-
- public void setSheetName(String sheetName) {
- this.sheetName = sheetName;
- }
-
- public String[] getColumnNames() {
- return columnNames;
- }
-
- public void setColumnNames(String[] columnNames) {
- this.columnNames = columnNames;
- }
-
- public String[] getPropertyNames() {
- return propertyNames;
- }
-
- public void setPropertyNames(String[] propertyNames) {
- this.propertyNames = propertyNames;
- }
-
- public String[] getCLabels() {
- return cLabels;
- }
-
- public void setCLabels(String[] labels) {
- cLabels = labels;
- }
-
- public int getRpp() {
- return rpp;
- }
-
- public void setRpp(int rpp) {
- this.rpp = rpp;
- }
-
- public HSSFCellStyle getStyle() {
- return style;
- }
-
- public void setStyle(HSSFCellStyle style) {
- this.style = style;
- }
-
- @SuppressWarnings("rawtypes")
- public List getResultList() {
- return resultList;
- }
-
- @SuppressWarnings("rawtypes")
- public void setResultList(List resultList) {
- this.resultList = resultList;
- }
- }
- package com.poi;
-
- import java.io.IOException;
- import java.io.OutputStream;
- import java.io.UnsupportedEncodingException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
-
- import javax.servlet.http.HttpServletResponse;
-
- 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.HSSFRichTextString;
- 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.Region;
-
-
-
-
-
-
-
-
-
-
-
-
-
- public class ExportExcelUtil {
-
-
-
-
-
-
-
- @SuppressWarnings("rawtypes")
- public static void exportExcel(ExcelEntity object, OutputStream outStream) throws Exception {
- HSSFWorkbook wb = new HSSFWorkbook();
- HSSFSheet sheet = wb.createSheet(object.getSheetName());
- HSSFRow row = sheet.createRow(0);
- HSSFCell cell = row.createCell(0);
- cell.setCellValue("序号");
- String[] colNames = object.getColumnNames();
- String[] propertys = object.getPropertyNames();
- for (int i = 0; i < colNames.length; i++) {
- row.createCell(i + 1).setCellValue(colNames[i]);
- }
- Iterator it = object.getResultList().iterator();
- int rowNum = 1;
- while (it.hasNext()) {
- Map map = (Map) it.next();
- HSSFRow rw = sheet.createRow(rowNum);
- rw.createCell(0).setCellValue(rowNum);
- rowNum++;
- for (int x = 0; x < propertys.length; x++) {
- String property = propertys[x];
- if (map.containsKey(property)) {
- Object value = map.get(propertys[x]);
- if (value == null || "null".equalsIgnoreCase(value.toString())) {
- value = "";
- }
- rw.createCell(x + 1).setCellValue(value + "");
- } else {
- rw.createCell(x + 1).setCellValue("");
- }
- }
- }
- try {
- wb.write(outStream);
- outStream.flush();
- outStream.close();
- } catch (IOException e) {
- if (outStream != null) {
- outStream.close();
- }
- e.printStackTrace();
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
- @SuppressWarnings("rawtypes")
- public static void exportView(HttpServletResponse response, List<Map> list, String[] columns, String[] propertyNames) throws IOException, UnsupportedEncodingException, Exception {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
- ExcelEntity entity = new ExcelEntity();
- entity.setColumnNames(columns);
- entity.setPropertyNames(propertyNames);
- entity.setResultList(list);
- entity.setSheetName(sdf.format(new Date()));
-
- OutputStream outStream = response.getOutputStream();
- response.setContentType("application/vnd.ms-excel;charset=UTF-8");
- String fileName = sdf.format(new Date()) + ".xls";
- response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("GBK"), "iso8859-1"));
- exportExcel(entity, outStream);
- }
-
-
-
-
-
-
-
-
-
- @SuppressWarnings("deprecation")
- public void createNormalHead(String headString, int colSum, String sheetName) {
- HSSFWorkbook wb = new HSSFWorkbook();
- HSSFSheet sheet = wb.createSheet(sheetName);
- HSSFRow row = sheet.createRow(0);
-
-
- HSSFCell cell = row.createCell(0);
- row.setHeight((short) 400);
-
-
- cell.setCellType(HSSFCell.ENCODING_UTF_16);
- cell.setCellValue(new HSSFRichTextString(headString));
-
-
- sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) colSum));
-
- HSSFCellStyle cellStyle = wb.createCellStyle();
-
- cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
- cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
- cellStyle.setWrapText(true);
-
-
- HSSFFont font = wb.createFont();
- font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
- font.setFontName("宋体");
- font.setFontHeight((short) 300);
- cellStyle.setFont(font);
-
- cell.setCellStyle(cellStyle);
- }
- }
- package com.poi;
-
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- import org.apache.commons.io.FilenameUtils;
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- import org.apache.poi.ss.usermodel.Cell;
- import org.apache.poi.ss.usermodel.DateUtil;
- import org.apache.poi.ss.usermodel.Row;
- import org.apache.poi.ss.usermodel.Sheet;
- import org.apache.poi.ss.usermodel.Workbook;
- import org.apache.poi.ss.util.CellReference;
- import org.apache.poi.xssf.usermodel.XSSFWorkbook;
-
-
-
-
-
-
-
-
-
-
-
-
-
- public class ReadExcel {
-
-
-
- private final static String XLS = "xls";
-
-
-
-
- private final static String XLSX = "xlsx";
-
-
-
-
-
-
-
-
-
-
- public static List<Map<String, Object>> exportListFromExcel(File file, int sheetNum) throws IOException {
- return exportListFromExcel(new FileInputStream(file), FilenameUtils.getExtension(file.getName()), sheetNum);
- }
-
-
-
-
-
-
-
-
-
-
- public static List<Map<String, Object>> exportListFromExcel(InputStream is, String extensionName, int sheetNum) throws IOException {
-
- Workbook workbook = null;
-
- if (extensionName.toLowerCase().equals(XLS)) {
- workbook = new HSSFWorkbook(is);
- } else if (extensionName.toLowerCase().equals(XLSX)) {
- workbook = new XSSFWorkbook(is);
- }
-
- return readCell(workbook, sheetNum);
- }
-
-
-
-
-
-
-
- public static List<Map<String, Object>> readCell(Workbook workbook, int sheetNum) {
- Sheet sheet = workbook.getSheetAt(sheetNum);
-
-
-
-
- List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
-
-
-
- for (int i = 1; i <= sheet.getLastRowNum(); i++) {
- Row row = sheet.getRow(i);
- Map<String, Object> map = new HashMap<String, Object>();
-
- for (Cell cell : row) {
-
-
- CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex());
- String key = cellRef.formatAsString();
-
- switch (cell.getCellType()) {
-
- case Cell.CELL_TYPE_STRING:
- map.put(key, cell.getRichStringCellValue().getString());
- break;
-
- case Cell.CELL_TYPE_NUMERIC:
- if (DateUtil.isCellDateFormatted(cell)) {
- map.put(key, cell.getDateCellValue());
- } else {
- map.put(key, cell.getNumericCellValue());
- }
- break;
-
- case Cell.CELL_TYPE_BOOLEAN:
- map.put(key, cell.getBooleanCellValue());
- break;
-
- case Cell.CELL_TYPE_FORMULA:
- map.put(key, cell.getCellFormula());
- break;
- case Cell.CELL_TYPE_BLANK:
- break;
- case Cell.CELL_TYPE_ERROR:
- break;
-
- default:
- map.put(key, "");
- }
- }
- list.add(map);
- }
- return list;
-
- }
-
- public static void main(String[] args) throws IOException {
-
- String paths = ReadExcel.class.getResource("c:\\excel.xlsx").getFile();
- List<Map<String, Object>> lists = ReadExcel.exportListFromExcel(new File(paths), 0);
- System.out.println(lists);
- }
- }
转载自blog.csdn.net/u013218587/article/details/77575228