如何将POI大数据高效导入Excel进行长尾词分析?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1338个文字,预计阅读时间需要6分钟。
javaPoi大数据导入导出,详情见附件 ExcelHelper.java
poi大数据导入导出详见附件 ExcelHelper.java
import java.io.FileOutputStream; import java.io.OutputStream; import java.util.Calendar; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.xssf.streaming.SXSSFWorkbook; /** * 每月导出数据 * @author admin * */ public class ExcelHelper { public void sysnMouthData (){ System.out.println("开始导出数据"); /*从数据库中查数据 */ Calendar cal = Calendar.getInstance(); int month = cal.get(Calendar.MONTH); int year = cal.get(Calendar.YEAR); String date = null;// 上一月份 if (month < 10) { date = "" + year + 0 + month + "%"; } else { date = "" + year + month + "%"; } // 创建一个Excel文件 SXSSFWorkbook workbook = new SXSSFWorkbook();//SXSSFWorkbook是poi3.8之后采用的专门处理大数据到execl中 Sheet sheet1 = workbook.createSheet("sheet1"); // 添加表头行 Row hssfRow1 = sheet1.createRow(0); // 设置单元格格式居中 cellStyle = workbook.createCellStyle(); cellStyle.setAlignment(CellStyle.ALIGN_CENTER); // 添加表头内容 Cell headCell1 = hssfRow1.createCell(0); headCell1.setCellValue("表头1"); headCell1.setCellStyle(cellStyle); headCell1 = hssfRow1.createCell(1); headCell1.setCellValue("表头2"); headCell1.setCellStyle(cellStyle); headCell1 = hssfRow1.createCell(2); headCell1.setCellValue("表头3"); headCell1.setCellStyle(cellStyle); headCell1 = hssfRow1.createCell(3); headCell1.setCellValue("表头4"); headCell1.setCellStyle(cellStyle); List<> daiqs = null; /** * 数据量过大,但又不能分sheet保存时 分批处理 */ Map map = new HashMap(); map.put("date", date); int total = userDao.countall(date);//从数据库中查询总数据 int page = 1; int minnum = 0; int maxnum = 1; int pagesize = 100000; if (total > pagesize) { page = total / pagesize + 1; } for (int i = 1; i <= page; i++) { minnum = (i - 1) * pagesize; if (i == page) { maxnum = total; } else { maxnum = i * pagesize; } daiqs = userDao.findByTime( date, maxnum, minnum);//从数据库中查询出数据 // 将内存中的数据刷到硬盘中去,这样就不会造成内存溢出 sheet1.getRow(minnum); // 添加数据内容 for (int j = 0; j < daiqs.size(); j++) { hssfRow1 = sheet1.createRow(j + 1 + (i - 1) * pagesize); DaIllegalQuery daiq = daiqs.get(j); //System.out.println(j + 1 + (i - 1) * pagesize); // 创建单元格,并设置值 Cell cell = hssfRow1.createCell(0); cell.setCellValue(daiq.getUserPhone()); // 手机号 cell.setCellStyle(cellStyle); cell = hssfRow1.createCell(1); cell.setCellValue(daiq.getQueryDate()); // 操作时间 cell.setCellStyle(cellStyle); cell = hssfRow1.createCell(2); cell.setCellValue("sdaf"+j); cell.setCellStyle(cellStyle); cell = hssfRow1.createCell(3); if (daiq.getOpercteType().trim().equals("1")) { cell.setCellValue("WEB_WAP"); } else if (daiq.getOpercteType().trim().equals("2")) { cell.setCellValue("WXCS_APP"); } else if (daiq.getOpercteType().trim().equals("3")) { cell.setCellValue("ANDROID_APP"); } else if (daiq.getOpercteType().trim().equals("4")) { cell.setCellValue("IOS_APP"); } else if (daiq.getOpercteType().trim().equals("5")) { cell.setCellValue("12580"); } cell.setCellStyle(cellStyle); cell = hssfRow1.createCell(4); cell.setCellValue(j); cell.setCellStyle(cellStyle); } if (i == page) { // 这里需要将最后剩下的数据也要刷到硬盘中去 sheet1.getRow(total - minnum * page); } } // 保存Excel文件 try { OutputStream outputStream = new FileOutputStream("test" + year + month + ".xlsx"); workbook.write(outputStream); outputStream.close(); } catch (Exception e) { e.printStackTrace(); } } } poi解析execl文件XML的方式
详见附件 ExampleEventUserModelUtil.java
import java.io.BufferedWriter;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.BuiltinFormats;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.model.SharedStringsTable;
import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;
public class ExampleEventUserModelUtil {
private static StylesTable stylesTable;
/**
* 处理一个sheet
* @param filename
* @throws Exception
*/
public void processOneSheet(String filename) throws Exception {
OPCPackage pkg = OPCPackage.open(filename);
XSSFReader r = new XSSFReader( pkg );
stylesTable = r.getStylesTable();
SharedStringsTable sst = r.getSharedStringsTable();
XMLReader parser = fetchSheetParser(sst);
Iterator
本文共计1338个文字,预计阅读时间需要6分钟。
javaPoi大数据导入导出,详情见附件 ExcelHelper.java
poi大数据导入导出详见附件 ExcelHelper.java
import java.io.FileOutputStream; import java.io.OutputStream; import java.util.Calendar; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.xssf.streaming.SXSSFWorkbook; /** * 每月导出数据 * @author admin * */ public class ExcelHelper { public void sysnMouthData (){ System.out.println("开始导出数据"); /*从数据库中查数据 */ Calendar cal = Calendar.getInstance(); int month = cal.get(Calendar.MONTH); int year = cal.get(Calendar.YEAR); String date = null;// 上一月份 if (month < 10) { date = "" + year + 0 + month + "%"; } else { date = "" + year + month + "%"; } // 创建一个Excel文件 SXSSFWorkbook workbook = new SXSSFWorkbook();//SXSSFWorkbook是poi3.8之后采用的专门处理大数据到execl中 Sheet sheet1 = workbook.createSheet("sheet1"); // 添加表头行 Row hssfRow1 = sheet1.createRow(0); // 设置单元格格式居中 cellStyle = workbook.createCellStyle(); cellStyle.setAlignment(CellStyle.ALIGN_CENTER); // 添加表头内容 Cell headCell1 = hssfRow1.createCell(0); headCell1.setCellValue("表头1"); headCell1.setCellStyle(cellStyle); headCell1 = hssfRow1.createCell(1); headCell1.setCellValue("表头2"); headCell1.setCellStyle(cellStyle); headCell1 = hssfRow1.createCell(2); headCell1.setCellValue("表头3"); headCell1.setCellStyle(cellStyle); headCell1 = hssfRow1.createCell(3); headCell1.setCellValue("表头4"); headCell1.setCellStyle(cellStyle); List<> daiqs = null; /** * 数据量过大,但又不能分sheet保存时 分批处理 */ Map map = new HashMap(); map.put("date", date); int total = userDao.countall(date);//从数据库中查询总数据 int page = 1; int minnum = 0; int maxnum = 1; int pagesize = 100000; if (total > pagesize) { page = total / pagesize + 1; } for (int i = 1; i <= page; i++) { minnum = (i - 1) * pagesize; if (i == page) { maxnum = total; } else { maxnum = i * pagesize; } daiqs = userDao.findByTime( date, maxnum, minnum);//从数据库中查询出数据 // 将内存中的数据刷到硬盘中去,这样就不会造成内存溢出 sheet1.getRow(minnum); // 添加数据内容 for (int j = 0; j < daiqs.size(); j++) { hssfRow1 = sheet1.createRow(j + 1 + (i - 1) * pagesize); DaIllegalQuery daiq = daiqs.get(j); //System.out.println(j + 1 + (i - 1) * pagesize); // 创建单元格,并设置值 Cell cell = hssfRow1.createCell(0); cell.setCellValue(daiq.getUserPhone()); // 手机号 cell.setCellStyle(cellStyle); cell = hssfRow1.createCell(1); cell.setCellValue(daiq.getQueryDate()); // 操作时间 cell.setCellStyle(cellStyle); cell = hssfRow1.createCell(2); cell.setCellValue("sdaf"+j); cell.setCellStyle(cellStyle); cell = hssfRow1.createCell(3); if (daiq.getOpercteType().trim().equals("1")) { cell.setCellValue("WEB_WAP"); } else if (daiq.getOpercteType().trim().equals("2")) { cell.setCellValue("WXCS_APP"); } else if (daiq.getOpercteType().trim().equals("3")) { cell.setCellValue("ANDROID_APP"); } else if (daiq.getOpercteType().trim().equals("4")) { cell.setCellValue("IOS_APP"); } else if (daiq.getOpercteType().trim().equals("5")) { cell.setCellValue("12580"); } cell.setCellStyle(cellStyle); cell = hssfRow1.createCell(4); cell.setCellValue(j); cell.setCellStyle(cellStyle); } if (i == page) { // 这里需要将最后剩下的数据也要刷到硬盘中去 sheet1.getRow(total - minnum * page); } } // 保存Excel文件 try { OutputStream outputStream = new FileOutputStream("test" + year + month + ".xlsx"); workbook.write(outputStream); outputStream.close(); } catch (Exception e) { e.printStackTrace(); } } } poi解析execl文件XML的方式
详见附件 ExampleEventUserModelUtil.java
import java.io.BufferedWriter;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.BuiltinFormats;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.model.SharedStringsTable;
import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;
public class ExampleEventUserModelUtil {
private static StylesTable stylesTable;
/**
* 处理一个sheet
* @param filename
* @throws Exception
*/
public void processOneSheet(String filename) throws Exception {
OPCPackage pkg = OPCPackage.open(filename);
XSSFReader r = new XSSFReader( pkg );
stylesTable = r.getStylesTable();
SharedStringsTable sst = r.getSharedStringsTable();
XMLReader parser = fetchSheetParser(sst);
Iterator

