//導入execljava
package syncDataSys;apache
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;xss
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;fetch
import syncDataSys.dao.DaIllegalQueryDao;
import syncDataSys.dao.DaUserDao;
import syncDataSys.dao.impl.DaIllegalQueryDaoImpl;
import syncDataSys.dao.impl.DaUserDaoImpl;
import syncDataSys.entity.DaIllegalQuery;
import syncDataSys.entity.DaUser;ui
public class ExcelHelper {this
public void sysnMouthData (){
System.out.println("開始導出數據");
DaUserDao userDao=new DaUserDaoImpl();
DaIllegalQueryDao daIllegalQueryDao=new DaIllegalQueryDaoImpl();
Calendar cal = Calendar.getInstance();
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);
// System.out.println("Current Date: " + cal.getTime()); // 當前時間
// System.out.println("Month: " + month);// 上一月份
// System.out.println("Year: " + year);// 當前年份
String date = null;// 上一月份
if (month < 10) {
date = "" + year + 0 + month + "%";
} else {
date = "" + year + month + "%";.net
}
// SimpleDateFormat s = new SimpleDateFormat("yyyymmdd-hhmmss");
List<DaUser> l = userDao.findUserbyTime(date); excel
Map map = new HashMap();
map.put("date", date);
int total = daIllegalQueryDao.countall(date);
int page = 1;orm
int minnum = 0;
int maxnum = 1;
int pagesize = 100000;
if (total > pagesize) {
page = total / pagesize + 1;
}xml
// 設置單元格格式居中
cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
// 建立一個工做表
// 建立一個Excel文件
// Workbook workbook1 = new XSSFWorkbook();
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("手機號");
headCell1.setCellStyle(cellStyle);
headCell1 = hssfRow1.createCell(1);
headCell1.setCellValue("操做時間");
headCell1.setCellStyle(cellStyle);
headCell1 = hssfRow1.createCell(2);
headCell1.setCellValue("變動記錄");
headCell1.setCellStyle(cellStyle);
headCell1 = hssfRow1.createCell(3);
headCell1.setCellValue("操做平臺");
headCell1.setCellStyle(cellStyle);
daiqs = daIllegalQueryDao.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(j);
cell.setCellStyle(cellStyle);
cell = hssfRow1.createCell(3);
cell.setCellValue("WEB_WAP");
cell.setCellStyle(cellStyle);
if (i == page) {
// 將內存中的數據刷到硬盤中去
sheet1.getRow(total - minnum * page);
}
}
// 保存Excel文件
try {
OutputStream outputStream = new FileOutputStream("d:\\test" + year
+ month + ".xlsx");
workbook.write(outputStream);
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
//從execl中導出數據
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<InputStream> sheets = r.getSheetsData();
while (sheets.hasNext())
{
InputStream sheet = sheets.next();
InputSource sheetSource = new InputSource(sheet);
parser.parse(sheetSource);
sheet.close();
}
}
/**
* 處理全部sheet
* @param filename
* @throws Exception
*/
public void processAllSheets(String filename) throws Exception {
OPCPackage pkg = OPCPackage.open(filename);
XSSFReader r = new XSSFReader( pkg );
SharedStringsTable sst = r.getSharedStringsTable();
XMLReader parser = fetchSheetParser(sst);
Iterator<InputStream> sheets = r.getSheetsData();
while(sheets.hasNext()) {
System.out.println("Processing new sheet:\n");
InputStream sheet = sheets.next();
InputSource sheetSource = new InputSource(sheet);
parser.parse(sheetSource);
sheet.close();
System.out.println("");
}
}
/**
* 獲取解析器
* @param sst
* @return
* @throws SAXException
*/
public XMLReader fetchSheetParser(SharedStringsTable sst) throws SAXException {
XMLReader parser =
XMLReaderFactory.createXMLReader(
"org.apache.xerces.parsers.SAXParser"
);
ContentHandler handler = new SheetHandler(sst);
parser.setContentHandler(handler);
return parser;
}
/**
* 自定義解析處理器
* See org.xml.sax.helpers.DefaultHandler javadocs
*/
private static class SheetHandler extends DefaultHandler {
private SharedStringsTable sst;
private String lastContents;
private boolean nextIsString;
private List<String> rowlist = new ArrayList<String>();
private int curRow = 0;
private int curCol = 0;
//定義前一個元素和當前元素的位置,用來計算其中空的單元格數量,如A6和A8等
private String preRef = null, ref = null;
//定義該文檔一行最大的單元格數,用來補全一行最後可能缺失的單元格
private String maxRef = null;
private CellDataType nextDataType = CellDataType.SSTINDEX;
private final DataFormatter formatter = new DataFormatter();
private short formatIndex;
private String formatString;
//用一個enum表示單元格可能的數據類型
enum CellDataType{
BOOL, ERROR, FORMULA, INLINESTR, SSTINDEX, NUMBER, DATE, NULL
}
private SheetHandler(SharedStringsTable sst) {
this.sst = sst;
}
/**
* 解析一個element的開始時觸發事件
*/
public void startElement(String uri, String localName, String name,
Attributes attributes) throws SAXException {
// c => cell
if(name.equals("c")) {
//前一個單元格的位置
if(preRef == null){
preRef = attributes.getValue("r");
}else{
preRef = ref;
}
//當前單元格的位置
ref = attributes.getValue("r");
this.setNextDataType(attributes);
// Figure out if the value is an index in the SST
String cellType = attributes.getValue("t");
if(cellType != null && cellType.equals("s")) {
nextIsString = true;
} else {
nextIsString = false;
}
}
// Clear contents cache
lastContents = "";
}
/**
* 根據element屬性設置數據類型
* @param attributes
*/
public void setNextDataType(Attributes attributes){
nextDataType = CellDataType.NUMBER;
formatIndex = -1;
formatString = null;
String cellType = attributes.getValue("t");
String cellStyleStr = attributes.getValue("s");
if ("b".equals(cellType)){
nextDataType = CellDataType.BOOL;
}else if ("e".equals(cellType)){
nextDataType = CellDataType.ERROR;
}else if ("inlineStr".equals(cellType)){
nextDataType = CellDataType.INLINESTR;
}else if ("s".equals(cellType)){
nextDataType = CellDataType.SSTINDEX;
}else if ("str".equals(cellType)){
nextDataType = CellDataType.FORMULA;
}
if (cellStyleStr != null){
int styleIndex = Integer.parseInt(cellStyleStr);
XSSFCellStyle style = stylesTable.getStyleAt(styleIndex);
formatIndex = style.getDataFormat();
formatString = style.getDataFormatString();
if ("m/d/yy" == formatString){
nextDataType = CellDataType.DATE;
//full format is "yyyy-MM-dd hh:mm:ss.SSS";
formatString = "yyyy-MM-dd";
}
if (formatString == null){
nextDataType = CellDataType.NULL;
formatString = BuiltinFormats.getBuiltinFormat(formatIndex);
}
}
}
/**
* 解析一個element元素結束時觸發事件
*/
public void endElement(String uri, String localName, String name)
throws SAXException {
// Process the last contents as required.
// Do now, as characters() may be called more than once
if(nextIsString) {
int idx = Integer.parseInt(lastContents);
lastContents = new XSSFRichTextString(sst.getEntryAt(idx)).toString();
nextIsString = false;
}
// v => contents of a cell
// Output after we've seen the string contents
if ("v".equals(name)|| "t".equals(name)) {
String value = this.getDataValue(lastContents.trim(), "");
//補全單元格之間的空單元格
if(!ref.equals(preRef)){
int len = countNullCell(ref, preRef);
for(int i=0;i<len;i++){
rowlist.add(curCol, "");
curCol++;
}
}
rowlist.add(curCol, value);
curCol++;
}else {
//若是標籤名稱爲 row,這說明已到行尾,調用 optRows() 方法
if (name.equals("row")) {
String value = "";
//默認第一行爲表頭,以該行單元格數目爲最大數目
if(curRow == 0){
maxRef = ref;
}
//補全一行尾部可能缺失的單元格
if(maxRef != null){
int len = countNullCell(maxRef, ref);
for(int i=0;i<=len;i++){
rowlist.add(curCol, "");
curCol++;
}
}
//拼接一行的數據
for(int i=0;i<rowlist.size();i++){
if(rowlist.get(i).contains(",")){
value += "\""+rowlist.get(i)+"\",";
}else{
value += rowlist.get(i)+",";
}
}
//加換行符
value += "\n";
// try {
// writer.write(value);
// } catch (IOException e) {
// e.printStackTrace();
// }
curRow++;
System.out.println(curRow + rowlist.toString()+"------");
//一行的末尾重置一些數據
rowlist.clear();
curCol = 0;
preRef = null;
ref = null;
}
}
}
/**
* 根據數據類型獲取數據
* @param value
* @param thisStr
* @return
*/
public String getDataValue(String value, String thisStr)
{
switch (nextDataType)
{
//這幾個的順序不能隨便交換,交換了極可能會致使數據錯誤
case BOOL:
char first = value.charAt(0);
thisStr = first == '0' ? "FALSE" : "TRUE";
break;
case ERROR:
thisStr = "\"ERROR:" + value.toString() + '"';
break;
case FORMULA:
thisStr = '"' + value.toString() + '"';
break;
case INLINESTR:
XSSFRichTextString rtsi = new XSSFRichTextString(value.toString());
thisStr = rtsi.toString();
rtsi = null;
break;
case SSTINDEX:
String sstIndex = value.toString();
thisStr = value.toString();
break;
case NUMBER:
if (formatString != null){
thisStr = formatter.formatRawCellContents(Double.parseDouble(value), formatIndex, formatString).trim();
}else{
thisStr = value;
}
thisStr = thisStr.replace("_", "").trim();
break;
case DATE:
try{
thisStr = formatter.formatRawCellContents(Double.parseDouble(value), formatIndex, formatString);
}catch(NumberFormatException ex){
thisStr = value.toString();
}
thisStr = thisStr.replace(" ", "");
break;
default:
thisStr = "";
break;
}
return thisStr;
}
/**
* 獲取element的文本數據
*/
public void characters(char[] ch, int start, int length)
throws SAXException {
lastContents += new String(ch, start, length);
}
/**
* 計算兩個單元格之間的單元格數目(同一行)
* @param ref
* @param preRef
* @return
*/
public int countNullCell(String ref, String preRef){
//excel2007最大行數是1048576,最大列數是16384,最後一列列名是XFD
String xfd = ref.replaceAll("\\d+", "");
String xfd_1 = preRef.replaceAll("\\d+", "");
xfd = fillChar(xfd, 3, '@', true);
xfd_1 = fillChar(xfd_1, 3, '@', true);
char[] letter = xfd.toCharArray();
char[] letter_1 = xfd_1.toCharArray();
int res = (letter[0]-letter_1[0])*26*26 + (letter[1]-letter_1[1])*26 + (letter[2]-letter_1[2]);
return res-1;
}
/**
* 字符串的填充
* @param str
* @param len
* @param let
* @param isPre
* @return
*/
String fillChar(String str, int len, char let, boolean isPre){
int len_1 = str.length();
if(len_1 <len){
if(isPre){
for(int i=0;i<(len-len_1);i++){
str = let+str;
}
}else{
for(int i=0;i<(len-len_1);i++){
str = str+let;
}
}
}
return str;
}
}
static BufferedWriter writer = null;
public static void main(String[] args) throws Exception {
ExampleEventUserModelUtil example = new ExampleEventUserModelUtil();
// String str = "Book1";
String filename = "D:\\jczl20177.xlsx";
System.out.println("-- 程序開始 --");
long time_1 = System.currentTimeMillis();
try{
// writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("C:\\users40.xlsx")));
example.processOneSheet(filename);
}finally{
// writer.close();
}
long time_2 = System.currentTimeMillis();
System.out.println("-- 程序結束 --");
System.out.println("-- 耗時 --"+(time_2 - time_1)+"ms");
}
}
//本項目因工做須要不能分sheet保存,根據本身的實際狀況 能夠選擇分sheet保存