POI 百万数据导出
poi 导出主类
package test;
import java.io.File;
import java.io.FileOutputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
public class ExcelExportSXXSSF {
// 定义工作表
private SXSSFWorkbook wb;
/**
* 定义工作表中的sheet
*/
private Sheet sh; /**
* 定义保存在内存中的数量,-1表示手动控制
*/
private int flushRows;
/** 导出文件行数 */
private int rownum;
/** 导出文件列数 */
private int colnum;
/** 导出文件的存放路径 */
private String filePath;
/** 下载导出文件的路径 */
private String fileWebPath;
/**文件名称前缀*/
private String filePrefix;
/**导出文件全路径*/
private String fileAllPath;
/** 导出文件列标题 */
private List<String> fieldNames;
/**导出文件每列代码,用于反射获取对象属性值*/
private List<String> fieldCodes;
private ExcelExportSXXSSF() {
}
/**
* 开始导出方法
*
* @param filePath
* 导出文件存放物理路径
* @param fileWebPath
* 导出文件web下载路径
* @param filePrefix
* 导出文件名的前缀
* @param flushRows
* 存放在内存的数据量
* @param fieldNames
* 导出文件列标题
* @param fieldCodes
* 导出数据对象的字段名称
* @param flushRows
* 写磁盘控制参数
* @return
*/
public static ExcelExportSXXSSF start(String filePath, String fileWebPath,String filePrefix,
List<String> fieldNames,List<String> fieldCodes, int flushRows) throws Exception {
ExcelExportSXXSSF excelExportSXXSSF = new ExcelExportSXXSSF();
excelExportSXXSSF.setFilePath(filePath);
excelExportSXXSSF.setFileWebPath(fileWebPath);
excelExportSXXSSF.setFilePrefix(filePrefix);
excelExportSXXSSF.setFieldNames(fieldNames);
excelExportSXXSSF.setFieldCodes(fieldCodes);
excelExportSXXSSF.setWb(new SXSSFWorkbook(flushRows));//创建workbook
excelExportSXXSSF.setSh(excelExportSXXSSF.getWb().createSheet());//创建sheet
excelExportSXXSSF.writeTitles();
return excelExportSXXSSF;
}
/**
* 设置导入文件的标题
* 开始生成导出excel的标题
* @throws Exception
*/
private void writeTitles() throws Exception {
rownum = 0;//第0行
colnum = fieldNames.size();//根据列标题得出列数
Row row = sh.createRow(rownum);
for (int cellnum = 0; cellnum < colnum; cellnum++) {
Cell cell = row.createCell(cellnum);
cell.setCellValue(fieldNames.get(cellnum));
}
}
/**
* 向导出文件写数据
*
* @param datalist
* 存放Object对象,仅支持单个自定义对象,不支持对象中嵌套自定义对象
* @return
*/
public void writeDatasByObject(List datalist) throws Exception {
for (int j = 0; j < datalist.size(); j++) {
rownum = rownum + 1;
Row row = sh.createRow(rownum);
for (int cellnum = 0; cellnum < fieldCodes.size(); cellnum++) {
Object owner = datalist.get(j);
Object value = invokeMethod(owner, fieldCodes.get(cellnum),
new Object[] {});
Cell cell = row.createCell(cellnum);
cell.setCellValue(value!=null?value.toString():"");
}
}
}
/**
* 向导出文件写数据
*
* @param datalist
* 存放字符串数组
* @return
*/
public void writeDatasByString(List<String> datalist) throws Exception {
rownum = rownum + 1;
Row row = sh.createRow(rownum);
int datalist_size = datalist.size();
for (int cellnum = 0; cellnum < colnum; cellnum++) {
Cell cell = row.createCell(cellnum);
if(datalist_size>cellnum){
cell.setCellValue(datalist.get(cellnum));
}else{
cell.setCellValue("");
}
}
}
/**
* 手动刷新方法,如果flushRows为-1则需要使用此方法手动刷新内存
*
* @param flushRows
* @throws Exception
*/
public void flush(int flushNum) throws Exception {
((SXSSFSheet) sh).flushRows(flushNum);
}
/**
* 导出文件
*
* @throws Exception
*/
public String exportFile() throws Exception {
String filename = filePrefix+"_"+MyUtil.getCurrentTimeStr() + ".xlsx";
File tmp = new File(filePath);
if (!tmp.exists()) { //判断文件是否存在
tmp.mkdirs();
}
FileOutputStream out = new FileOutputStream(tmp+filename);
wb.write(out);
out.flush();
out.close();
setFileAllPath(fileWebPath + filename); //设置路径
return fileWebPath + filename; //返回路径
}
/**
* 反射方法,通过get方法获取对象属性
*
* @param owner
* @param fieldname
* @param args
* @return
* @throws Exception
*/
private Object invokeMethod(Object owner, String fieldname, Object[] args)
throws Exception {
String methodName = "get" + fieldname.substring(0, 1).toUpperCase()
+ fieldname.substring(1);
Class ownerClass = owner.getClass();
Class[] argsClass = new Class[args.length];
for (int i = 0, j = args.length; i < j; i++) {
argsClass[i] = args[i].getClass();
}
Method method = ownerClass.getMethod(methodName, argsClass);
return method.invoke(owner, args);
}
public SXSSFWorkbook getWb() {
return wb;
}
public void setWb(SXSSFWorkbook wb) {
this.wb = wb;
}
public Sheet getSh() {
return sh;
}
public void setSh(Sheet sh) {
this.sh = sh;
} public int getFlushRows() {
return flushRows;
}
public void setFlushRows(int flushRows) {
this.flushRows = flushRows;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public String getFileWebPath() {
return fileWebPath;
}
public void setFileWebPath(String fileWebPath) {
this.fileWebPath = fileWebPath;
}
public List<String> getFieldNames() {
return fieldNames;
}
public void setFieldNames(List<String> fieldNames) {
this.fieldNames = fieldNames;
}
public List<String> getFieldCodes() {
return fieldCodes;
}
public void setFieldCodes(List<String> fieldCodes) {
this.fieldCodes = fieldCodes;
}
public int getRownum() {
return rownum;
}
public String getFilePrefix() {
return filePrefix;
}
public void setFilePrefix(String filePrefix) {
this.filePrefix = filePrefix;
}
public int getColnum() {
return colnum;
}
public String getFileAllPath() {
return fileAllPath;
}
public void setFileAllPath(String fileAllPath) {
this.fileAllPath = fileAllPath;
}
public static void main(String[] args) throws Exception {
/** 导出文件存放物理路径
* @param fileWebPath
* 导出文件web下载路径
* @param filePrefix
* 导出文件名的前缀
* @param flushRows
* 存放在内存的数据量
* @param fieldNames
* 导出文件列标题
* @param fieldCodes
* 导出数据对象的字段名称
* @param flushRows*/
//导出文件存放的路径,并且是虚拟目录指向的路径
String filePath = "d:/upload/linshi/";
//导出文件的前缀
String filePrefix="ypxx";
//-1表示关闭自动刷新,手动控制写磁盘的时机,其它数据表示多少数据在内存保存,超过的则写入磁盘
int flushRows=100;
//定义导出数据的title
List<String> fieldNames=new ArrayList<String>();
fieldNames.add("流水号");
fieldNames.add("通用名");
fieldNames.add("价格");
//告诉导出类数据list中对象的属性,让ExcelExportSXXSSF通过反射获取对象的值
List<String> fieldCodes=new ArrayList<String>();
fieldCodes.add("bm");//药品流水号
fieldCodes.add("mc");//通用名
fieldCodes.add("price");//价格
//注意:fieldCodes和fieldNames个数必须相同且属性和title顺序一一对应,这样title和内容才一一对应
//开始导出,执行一些workbook及sheet等对象的初始创建
ExcelExportSXXSSF excelExportSXXSSF = ExcelExportSXXSSF.start(filePath, "/upload/", filePrefix, fieldNames, fieldCodes, flushRows);
//准备导出的数据,将数据存入list,且list中对象的字段名称必须是刚才传入ExcelExportSXXSSF的名称
List<Ypxx> list = new ArrayList<Ypxx>();
for (int i=0; i<=1048574; i++) {
Ypxx ypxx1 = new Ypxx("001"+i, "青霉素"+i, i);
list.add(ypxx1);
}
//执行导出
excelExportSXXSSF.writeDatasByObject(list);
//输出文件,返回下载文件的http地址
String webpath = excelExportSXXSSF.exportFile();
System.out.println(webpath);
}
}
poi 所需要的工具类
package test;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MyUtil {
private static final String CNUMBER_PATTERN = "^[0-9]*$";// 判断数字的正则表达式
private static SimpleDateFormat bartDateFormat = new SimpleDateFormat(
"yyyy-MM-dd");
public static int getYear(Date date) {
try {
String form_date = bartDateFormat.format(date);
return Integer.parseInt(form_date.substring(0, 4));
} catch (Exception ex) {
return Integer.parseInt(getCurrentTime().substring(0, 4));
}
}
public static int getMonth(Date date) {
try {
String form_date = bartDateFormat.format(date);
return Integer.parseInt(form_date.substring(5, form_date
.lastIndexOf("-")));
} catch (Exception ex) {
String currenttimel = getCurrentTime();
return Integer.parseInt(currenttimel.substring(5, currenttimel
.lastIndexOf("-")));
}
}
public static int getDay(Date date) {
try {
String form_date = bartDateFormat.format(date);
return Integer.parseInt(form_date.substring(form_date
.lastIndexOf("-") + 1, form_date.length()));
} catch (Exception ex) {
String currenttimel = getCurrentTime();
return Integer.parseInt(currenttimel.substring(5, currenttimel
.lastIndexOf("-")));
}
}
public static String[] getMonth_days(Date date) {
String[] month_day = { "31", "28", "31", "30", "31", "30", "31", "31",
"30", "31", "30", "31" };
int year = getYear(date);
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
month_day[1] = "29";
return month_day;
}
public static int getCount(String str, String sign) {
if (str == null) {
return 0;
}
StringTokenizer s = new StringTokenizer(str, sign);
return s.countTokens();
}
public static String[] getArray(String str, String sign) {
int count = getCount(str, sign);
int j = 0;
String[] arr = new String[count];
for (int i = 0; i < count; i++) {
if (str.indexOf(sign) != -1) {
j = str.indexOf(sign);
arr[i] = str.substring(0, j);
str = str.substring(j + 1);
} else {
arr[i] = str;
}
// System.out.println(arr[i]);
}
return arr;
}
public static String getCurrentTime() {
java.sql.Timestamp temp = new java.sql.Timestamp(System
.currentTimeMillis());
return (temp.toString()).substring(0, 19);
}
public static Date toDateType(String s) {
DateFormat df = DateFormat.getDateInstance();
df.setLenient(false);
s = s.replace('/', '-');
s = s.replace('.', '.');
try {
return df.parse(s);
} catch (Exception e) {
return null;
}
}
public static String unicodeToGB(String strIn) {
byte[] b;
String strOut = null;
if (strIn == null || (strIn.trim()).equals("")) {
return strIn;
}
try {
b = strIn.getBytes("GBK");
strOut = new String(b, "ISO8859_1");
} catch (UnsupportedEncodingException e) {
}
return strOut;
}
public static String GBToUnicode(String strIn) {
String strOut = null;
if (strIn == null || (strIn.trim().equals(""))) {
return strIn;
}
try {
byte[] b = strIn.getBytes("ISO8859_1");
strOut = new String(b, "GBK");
} catch (Exception e) {
}
return strOut;
}
public static String UTFToUnicode(String strIn) {
String strOut = null;
if (strIn == null || (strIn.trim().equals(""))) {
return strIn;
}
try {
byte[] b = strIn.getBytes("ISO8859_1");
strOut = new String(b, "UTF-8");
} catch (Exception e) {
}
return strOut;
}
public static String UTFToGBK(String strIn) {
String strOut = null;
if (strIn == null || (strIn.trim().equals(""))) {
return strIn;
}
try {
byte[] b = strIn.getBytes("GBK");
strOut = new String(b, "UTF-8");
} catch (Exception e) {
}
return strOut;
}
public static String buildURL(Map parameters) {
StringBuffer url = new StringBuffer();
if (parameters != null && parameters.size() > 0) {
url.append("?");
for (Iterator iter = parameters.keySet().iterator(); iter.hasNext();) {
String key = (String) iter.next();
String[] values = (String[]) parameters.get(key);
for (int i = 0; i < values.length; i++) {
url.append(key).append("=").append(values[i]).append("&");
}
}
}
return url.toString();
}
/**
* 获得当前时间(根据格式字符串)
*
* @param format
* String 格式字符串
* @return String
*/
public static String getDateByFormat(String format) {
Calendar date = Calendar.getInstance();
//System.out.print("date.getTime()==="+date.getTime());
java.text.SimpleDateFormat sim = new java.text.SimpleDateFormat(format);
String str = sim.format(date.getTime());
return str;
}
/**
* 获得当前时间
*
* @return String
*/
public static String getDate() {
Calendar date = Calendar.getInstance();
java.text.SimpleDateFormat sim = new java.text.SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
String str = sim.format(date.getTime());
return str;
}
/**
* 获得当前时间
*
* @return Date
*/
public static Date getNowDate() {
/*Calendar date = Calendar.getInstance();
return date.getTime();*/
return getDate(getDate());
}
/**
* 字符串转换为时间
*
* @param date
* String
* @return Date
*/
public static Date getDate(String date) {
try {
SimpleDateFormat localTime = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
Date date1 = localTime.parse(date);
return date1;
}
catch (ParseException ex) {
ex.printStackTrace();
}
return null;
}
/**
* 字符串转换为时间2
*
* @param date
* String
* @return Date
*/
public static Date getDate2(String date) {
try {
SimpleDateFormat localTime = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = localTime.parse(date);
return date1;
}
catch (ParseException ex) {
ex.printStackTrace();
}
return null;
}
/**
* 字符串转换为时间3
*
* @param date
* String
* @return Date
*/
public static Date getDate3(String date) {
try {
SimpleDateFormat localTime = new SimpleDateFormat("yyyy-MM");
Date date1 = localTime.parse(date);
return date1;
}
catch (ParseException ex) {
ex.printStackTrace();
}
return null;
}
/**
* 字符串转换为时间3
*
* @param date
* String
* @return Date
*/
public static Date getDateByHHMM(String date) {
try {
SimpleDateFormat localTime = new SimpleDateFormat("HH:mm");
Date date1 = localTime.parse(date);
return date1;
}
catch (ParseException ex) {
ex.printStackTrace();
}
return null;
}
/**
* 字符串转换为时间3
*
* @param date
* String
* @return Date
*/
public static Date getDate4(String date) {
try {
SimpleDateFormat localTime = new SimpleDateFormat("yyyy");
Date date1 = localTime.parse(date);
return date1;
}
catch (ParseException ex) {
ex.printStackTrace();
}
return null;
}
/**
* 取得秒数
*/
public static Long getDateDiff_Second(Date d1, Date d2) {
return (d2.getTime() - d1.getTime()) / 1000;
}
/**
* 取得分钟
*
* @param d1
* Date
* @param d2
* Date
* @return Long
*/
public static Long getDateDiff_Minute(Date d1, Date d2) {
return (d2.getTime() - d1.getTime()) / (1000 * 60);
}
/**
* 取得小时
*
* @param d1
* Date
* @param d2
* Date
* @return Long
*/
public static Long getDateDiff_Hour(Date d1, Date d2) {
return (d2.getTime() - d1.getTime()) / (1000 * 3600);
}
public static Long getDateDiff_Hour(String d1, String d2) {
return (getDate(d2).getTime() - getDate(d1).getTime()) / (1000 * 3600);
}
/**
* 取得天数
*
* @param d1
* Date
* @param d2
* Date
* @return Long
*/
public static Long getDateDiff_Day(Date d1, Date d2) {
return (d2.getTime() - d1.getTime()) / (1000 * 3600 * 24);
}
public static Long getDateDiff_Day(String d1, String d2) {
return (getDate(d2).getTime() - getDate(d1).getTime())
/ (1000 * 3600 * 24);
}
/**
* 取得星期间隔
*
* @param d1
* Date
* @param d2
* Date
* @return Long
*/
public static Long getDateDiff_Week(Date d1, Date d2) {
return (d2.getTime() - d1.getTime()) / (1000 * 3600 * 24 * 7);
}
/**
* 取得当前时间的 间隔多少小时之后的时间
*
* @param hour
* int
* @return String
*/
public static String getDateAdd(int hour) {
Calendar calendar = Calendar.getInstance();
java.text.SimpleDateFormat sd = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
calendar.set(Calendar.HOUR, hour + calendar.get(Calendar.HOUR));
String enddate = sd.format(calendar.getTime());
return enddate;
}
/**
* 取得当前时间的 间隔多少小时之后的时间
*
* @param hour
* int
* @return String
*/
public static String getDateAdd(String starttime, int hour) {
Calendar calendar = Calendar.getInstance();
java.text.SimpleDateFormat sd = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
calendar.setTime(getDate(starttime));
calendar.set(Calendar.HOUR, hour + calendar.get(Calendar.HOUR));
String date = sd.format(calendar.getTime());
return date;
}
public static Date getDateAdd(Date starttime, int hour) {
Calendar calendar = Calendar.getInstance();
java.text.SimpleDateFormat sd = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
calendar.setTime(starttime);
calendar.set(Calendar.HOUR, hour + calendar.get(Calendar.HOUR));
return calendar.getTime();
}
/**
* 取得当前时间的 间隔多少小时之后的时间
*
* @param hour
* int
* @return String
*/
public static String getDateAdd2(String starttime, int hour) {
Calendar calendar = Calendar.getInstance();
java.text.SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
calendar.setTime(getDate2(starttime));
calendar.set(Calendar.HOUR, hour + calendar.get(Calendar.HOUR));
String date = sd.format(calendar.getTime());
return date;
}
/**
* 取得当前时间的 间隔多少秒之后的时间
*
* @param hour
* int
* @return String
*/
public static String getDateAddSecond(String starttime, long second) {
Calendar calendar = Calendar.getInstance();
java.text.SimpleDateFormat sd = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
calendar.setTime(getDate2(starttime));
calendar.set(Calendar.SECOND, (int) (second + calendar
.get(Calendar.SECOND)));
String date = sd.format(calendar.getTime());
return date;
}
/**
* 获得时间格式的文件名称
*
* @return String
*/
public static String getFileName() {
Calendar date = Calendar.getInstance();
java.text.SimpleDateFormat sim = new java.text.SimpleDateFormat(
"yyyyMMdd_hhmmss");
String str = sim.format(date.getTime());
return str;
}
// 获得月日
public static String get_MM_DD(String s) {
java.text.SimpleDateFormat sim = new java.text.SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
Date date;
String str = "";
try {
date = sim.parse(s);
sim = new java.text.SimpleDateFormat("[MM-dd]");
str = sim.format(date.getTime());
} catch (ParseException e) {
e.printStackTrace();
} finally {
return str;
}
}
// 获得年月日
public static String get_YYYY_MM_DD(String s) {
java.text.SimpleDateFormat sim = new java.text.SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
Date date;
String str = "";
try {
date = sim.parse(s);
sim = new java.text.SimpleDateFormat("yyyy-MM-dd");
str = sim.format(date.getTime());
} catch (ParseException e) {
e.printStackTrace();
} finally {
return str;
}
}
// 获得年月
public static String get_YYYY_MM(String s) {
java.text.SimpleDateFormat sim = new java.text.SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
Date date;
String str = "";
try {
date = sim.parse(s);
sim = new java.text.SimpleDateFormat("yyyy-MM");
str = sim.format(date.getTime());
} catch (ParseException e) {
e.printStackTrace();
} finally {
return str;
}
}
// 获得年
public static String get_YYYY(String s) {
java.text.SimpleDateFormat sim = new java.text.SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
Date date;
String str = "";
try {
date = sim.parse(s);
sim = new java.text.SimpleDateFormat("yyyy");
str = sim.format(date.getTime());
} catch (ParseException e) {
e.printStackTrace();
} finally {
return str;
}
}
/**
* 检查IP是否合法
*
* @param value
* @return
*/
public static boolean ipValid(String s) {
String regex0 = "(2[0-4]\\d)" + "|(25[0-5])";
String regex1 = "1\\d{2}";
String regex2 = "[1-9]\\d";
String regex3 = "\\d";
String regex = "(" + regex0 + ")|(" + regex1 + ")|(" + regex2 + ")|("
+ regex3 + ")";
regex = "(" + regex + ").(" + regex + ").(" + regex + ").(" + regex
+ ")";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(s);
return m.matches();
}
/**
* 将java.util.Date 格式转换为字符串格式'yyyy-MM-dd HH:mm:ss'(24小时制)<br>
* 如Sat May 11 17:24:21 CST 2002 to '2002-05-11 17:24:21'<br>
*
* @param time
* Date 日期<br>
* @return String 字符串<br>
*/
public static String dateToString(Date time) {
SimpleDateFormat formatter;
formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String ctime = formatter.format(time);
return ctime;
}
public static String dateToString2(Date time) {
SimpleDateFormat formatter;
formatter = new SimpleDateFormat("yyyy-MM-dd");
String ctime = formatter.format(time);
return ctime;
}
public static String yymmdddateToString(Date time) {
SimpleDateFormat formatter;
formatter = new SimpleDateFormat("yyyyMMdd");
String ctime = formatter.format(time);
return ctime;
}
public static String hhmmssdateToString(Date time) {
SimpleDateFormat formatter;
formatter = new SimpleDateFormat("hhmmss");
String ctime = formatter.format(time);
return ctime;
}
/**
* 将java.util.Date 格式转换为字符串格式'yyyy-MM-dd HH:mm:ss a'(12小时制)<br>
* 如Sat May 11 17:23:22 CST 2002 to '2002-05-11 05:23:22 下午'<br>
*
* @param time
* Date 日期<br>
* @param x
* int 任意整数如:1<br>
* @return String 字符串<br>
*/
public static String dateToString(Date time, int x) {
SimpleDateFormat formatter;
formatter = new SimpleDateFormat("yyyy-MM-dd KK:mm:ss a");
String ctime = formatter.format(time);
return ctime;
}
// 判断字符串是否为Null或“”
public static boolean isNotNullAndEmpty(String str) {
if (null != str && !str.equals("")) {
return true;
}
return false;
}
// 判断列表是否为Null或大小为0
public static boolean isNotNullAndZero(List list) {
if (null != list && list.size() > 0) {
return true;
}
return false;
}
// 判断字符串是否为Null,trim之后判断是否为空
public static boolean isNotNullAndEmptyByTrim(String str) {
if (null != str && !str.trim().equals("")) {
return true;
}
return false;
}
// 删除ArrayList中重复元素
public static List removeDuplicate(List list) {
HashSet h = new HashSet(list);
list.clear();
list.addAll(h);
return list;
}
// 判断当前天是否为月末时间即加一天是否为下个月
public static boolean isTheLastMonthDAY() {
String curtime = getDate();
String nexttime = getDateAdd(curtime, 24);
if (!get_YYYY_MM(curtime).equals(get_YYYY_MM(nexttime))) {
return true;
}
return false;
}
/**
* 编码转换
*
* @param str
* @return
*/
public static String getReadStr(String str) {
try {
String temp_p = str;
byte[] temp_t = temp_p.getBytes("ISO-8859-1");
String unicode = new String(temp_t, "gb2312");
return unicode;
} catch (Exception e) {
return "null";
}
}
public static String isoTOUTF(String str) {
try {
String temp_p = str;
byte[] temp_t = temp_p.getBytes("ISO-8859-1");
String unicode = new String(temp_t, "UTF-8");
return unicode;
} catch (Exception e) {
return "null";
}
}
public static String utfTOISO(String str) {
try {
String temp_p = str;
byte[] temp_t = temp_p.getBytes("UTF-8");
String unicode = new String(temp_t, "ISO-8859-1");
return unicode;
} catch (Exception e) {
return "null";
}
}
public static String toHtml(String str) {
if (str == null) {
return "";
} else {
str = str.replaceAll("<", "&lt;");
str = str.replaceAll(">", "&gt;");
str = str.replaceAll("'", "''");
str = str.replaceAll(" ", "&nbsp;");
str = str.replaceAll("\n", "<br>");
}
return str;
}
public static String toText(String str) {
if (str == null) {
return "";
} else {
str = str.replaceAll("&lt;", "<");
str = str.replaceAll("&gt;", ">");
str = str.replaceAll("''", "'");
str = str.replaceAll("&nbsp;", " ");
str = str.replaceAll("<br>", "\n");
}
return str;
}
/**
* 去掉字符串的html代码
*
* @param htmlStr
* 字符串
* @return 结果
*/
public static String htmlToStr(String htmlStr) {
String result = "";
boolean flag = true;
if (htmlStr == null) {
return null;
}
char[] a = htmlStr.toCharArray();
int length = a.length;
for (int i = 0; i < length; i++) {
if (a[i] == '<') {
flag = false;
continue;
}
if (a[i] == '>') {
flag = true;
continue;
}
if (flag == true) {
result += a[i];
}
}
return result.toString().replaceAll("&nbsp;", " ");
}
/**
* 去掉字符串里面的html代码。<br>
* 要求数据要规范,比如大于小于号要配套,否则会被集体误杀。
*
* @paramcontent 内容
* @return去掉后的内容
*/
public static String stripHtml(String content) {
content = content.replaceAll("<p.*?>", "rn");
content = content.replaceAll("<brs*/?>", "rn");
content = content.replaceAll("<.*?>", "");
content = content.replaceAll("&nbsp;", " ");
return content;
}
/**
* 判断某个数据是否在已知的List中存在的方法 存在则返回true否则返回faluse
*
* @param List
* 已知的List集合
* @param Strng
* 已知的字符串
* @return boolean true/false
*/
public static boolean theDateIsInThelist(List thelist, String thestring) {
if (null != thelist && thelist.size() > 0) {
if (null != thestring) {
for (int i = 0; i < thelist.size(); i++) {
String curstring = (String) thelist.get(i);
if (thestring.equals(curstring)) {
return true;
}
}
}
}
return false;
}
/**
* 将短时间格式字符串转换为时间 yyyy-MM-dd
*
* @param strDate
* @return
*/
public static Date strToDate(String strDate) throws Exception {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
/**
*@param ms
* 为要转换的秒数
*@return 返回h m s数组
*/
public static long[] toHMS(long ms) {
long d;// 天
long s;// 秒
long h;// 小时
long m;// 分钟
d = ms / 3600 / 24;
h = (ms - d * 24 * 3600) / 3600;
m = (ms - h * 60 * 60 - d * 24 * 3600) / 60;
s = ms - h * 60 * 60 - m * 60 - d * 24 * 3600;
return new long[] { d, h, m, s };
}
/**
* 获取当前时间的字符串 格式为"yyyyMMddHHmmss"
* @return 返回获取的当前时间
*/
public static String getCurrentTimeStr() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String curDate = sdf.format(new Date());
return curDate;
}
/**
*
* @return
*/
public static String getStartTimeStr(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String startDate = sdf.format(new Date(0));
return startDate;
}
public static boolean isInteger(String value){
/*Pattern p = Pattern.compile(CNUMBER_PATTERN);
Matcher m = p.matcher(value);
return m.find();*/
try{
java.lang.Integer.parseInt(value);
}catch(Exception e){
return false;
}
return true;
}
public static boolean isFlost(String value){
try{
java.lang.Float.parseFloat(value);
}catch(Exception e){
return false;
}
return true;
}
public static boolean isDate(String value,String formatstr){
try{
SimpleDateFormat formatter = new SimpleDateFormat(formatstr);
formatter.setLenient(false);
formatter.parse(value);
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}
}
public static void main(String[] args) {
/* BigDecimal b1 = new BigDecimal("4315813000");
BigDecimal b2 = new BigDecimal(1000);
BigDecimal b3 = b1.divide(b2, 0, BigDecimal.ROUND_HALF_UP);
System.out.println("b3===" + b3);
long[] hms = toHMS(b3.longValue());
System.out.println(hms[0] + " D " + hms[1] + " H " + hms[2]
+ "M" + hms[3] + "S");*/
/*
ArrayList list = new ArrayList();
String mib = "1.2.3";
list.add("1.2.3.1.0");
list.add("1.2.3.5");
list.add("1.2.3.3.0");
list.add("1.2.3.8");
list.add("1.2.3.12");
List lastvaluelt= new ArrayList();
List lostlist = new ArrayList();
for(int i = 0;i<list.size();i++){
String curmib = (String) list.get(i);
String curvalue_andend = curmib.substring(mib.length() + 1,
curmib.length());
String curvalue="";
if(curvalue_andend.indexOf(".")!=-1){
curvalue = curvalue_andend.substring(0, curvalue_andend
.indexOf("."));
System.out.println("teststst=="+curvalue);
}else{
curvalue=curvalue_andend;
}
lastvaluelt.add(curvalue);
}
Comparator comp = new ContentComparator();
Collections.sort(lastvaluelt,comp);
int theMaxValue = Integer.valueOf((String) lastvaluelt.get(lastvaluelt.size()-1));
for(int i = 1;i<theMaxValue;i++){
boolean flag = false;
for(int j = 0;j<lastvaluelt.size();j++){
String curval = (String) lastvaluelt.get(j);
if(Integer.parseInt(curval)==i){
flag = true;
break;
}
}
if(!flag){
lostlist.add(i);
}
}
for (int i = 0; i < lostlist.size(); i++) {
System.out.println(lostlist.get(i));
}
*/
//MyUtil.getStartTimeStr();
}
}
实体类(需要的数据封装在这里)
package test;
import java.util.Date;
public class Ypxx {
private String bm;
private String mc;
private float price;
private String kkk;
private String pzwh;
private Date pzwhyxq;
private String jky;
private String bzcz;
private String bzdw;
private Double lsjg;
private String lsjgcc;
private String zlcc;
private String zlccsm;
private String ypjybg;
private String ypjybgbm;
private Date ypjybgyxq;
private String jyzt;
private String vchar1;
public Ypxx(String bm,String mc,float price){
this.bm = bm;
this.mc = mc;
this.price = price;
}
public String getKkk() {
return kkk;
}
public void setKkk(String kkk) {
this.kkk = kkk;
}
public String getPzwh() {
return pzwh;
}
public void setPzwh(String pzwh) {
this.pzwh = pzwh;
}
public Date getPzwhyxq() {
return pzwhyxq;
}
public void setPzwhyxq(Date pzwhyxq) {
this.pzwhyxq = pzwhyxq;
}
public String getJky() {
return jky;
}
public void setJky(String jky) {
this.jky = jky;
}
public String getBzcz() {
return bzcz;
}
public void setBzcz(String bzcz) {
this.bzcz = bzcz;
}
public String getBzdw() {
return bzdw;
}
public void setBzdw(String bzdw) {
this.bzdw = bzdw;
}
public Double getLsjg() {
return lsjg;
}
public void setLsjg(Double lsjg) {
this.lsjg = lsjg;
}
public String getLsjgcc() {
return lsjgcc;
}
public void setLsjgcc(String lsjgcc) {
this.lsjgcc = lsjgcc;
}
public String getZlcc() {
return zlcc;
}
public void setZlcc(String zlcc) {
this.zlcc = zlcc;
}
public String getZlccsm() {
return zlccsm;
}
public void setZlccsm(String zlccsm) {
this.zlccsm = zlccsm;
}
public String getYpjybg() {
return ypjybg;
}
public void setYpjybg(String ypjybg) {
this.ypjybg = ypjybg;
}
public String getYpjybgbm() {
return ypjybgbm;
}
public void setYpjybgbm(String ypjybgbm) {
this.ypjybgbm = ypjybgbm;
}
public Date getYpjybgyxq() {
return ypjybgyxq;
}
public void setYpjybgyxq(Date ypjybgyxq) {
this.ypjybgyxq = ypjybgyxq;
}
public String getJyzt() {
return jyzt;
}
public void setJyzt(String jyzt) {
this.jyzt = jyzt;
}
public String getVchar1() {
return vchar1;
}
public void setVchar1(String vchar1) {
this.vchar1 = vchar1;
}
public String getBm() {
return bm;
}
public void setBm(String bm) {
this.bm = bm;
}
public String getMc() {
return mc;
}
public void setMc(String mc) {
this.mc = mc;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
@Override
public String toString() {
return "Ypxx [bm=" + bm + ", mc=" + mc + ", price=" + price + "]";
}
}
POI 百万数据导出的更多相关文章
- Java使用POI实现数据导出excel报表
Java使用POI实现数据导出excel报表 在上篇文章中,我们简单介绍了java读取word,excel和pdf文档内容 ,但在实际开发中,我们用到最多的是把数据库中数据导出excel报表形式.不仅 ...
- 使用POI实现数据导出Excel表格
package cn.sh.bzt.kwj.action; import java.io.IOException; import java.io.OutputStream; import java.t ...
- java使用POI将数据导出放入Excel
本文主要是将数据库取出的数据按照自定义的行列格式导出到excel中,POI则是实现我们需求所用到的技术. POI介绍 使用spring boot导入相关依赖 获取数据(自行处理) 完整代码实例:创建e ...
- SpringBoot图文教程10—模板导出|百万数据Excel导出|图片导出「easypoi」
有天上飞的概念,就要有落地的实现 概念十遍不如代码一遍,朋友,希望你把文中所有的代码案例都敲一遍 先赞后看,养成习惯 SpringBoot 图文教程系列文章目录 SpringBoot图文教程1「概念+ ...
- 使用JDBC+POI把Excel中的数据导出到MySQL
POI是Apache的一套读MS文档的API,用它还是可以比较方便的读取Office文档的.目前支持Word,Excel,PowerPoint生成的文档,还有Visio和Publisher的. htt ...
- java操作Excel之POI(4)利用POI实现数据的批量导出
后台导出方法: /** * 后台导出方法 * 利用POI实现数据的批量导出 */ public String export() throws Exception{ Connection con = n ...
- 项目一:第十四天 1.在realm中动态授权 2.Shiro整合ehcache 缓存realm中授权信息 3.动态展示菜单数据 4.Quartz定时任务调度框架—Spring整合javamail发送邮件 5.基于poi实现分区导出
1 Shiro整合ehCache缓存授权信息 当需要进行权限校验时候:四种方式url拦截.注解.页面标签.代码级别,当需要验证权限会调用realm中的授权方法 Shiro框架内部整合好缓存管理器, ...
- POI SXSSF API 导出1000万数据示例
SXSSF是XSSF API的兼容流式扩展,在必须生成非常大的电子表格.并且堆空间有限时使用. SXSSF通过限制对滑动窗口内数据的访问实现低内存占用,而XSSF允许访问文档中的所有行. 不在窗口中的 ...
- 使用POI把查询到的数据表数据导出到Excel中,一个表一个sheet.最详细!!!
一.需求 我们会遇到开发任务: 经理:小王,你来做一下把数据库里的数据导出到Excel中,一个表是一个sheet,不要一个表一个Excel. 小王:好的,经理.(内心一脸懵逼) 二.前期准备 首先我们 ...
随机推荐
- [浪风前端开发]JS获取当前时间戳的方法
由于最近在研究轻交互式web设计,所以整理了下面的东东,仅供分享测试学习交流之用. JavaScript 获取当前时间戳:第一种方法: var timestamp = Date.parse(new D ...
- 检测进程不存在自动重启shell脚本
#!/bin/bash WORKDIR="/usr/local/gse/gseagent" [[ -d $WORKDIR ]] && { if ! ps aux|g ...
- Laravel5.1 数据库--DB运行原生SQL
Laravel操作数据库有三种:DB原生SQL.构建器.Model.这三种依情况而决定使用哪种更合适. 那么今儿咱就从DB原生SQL说起: 1 用DB门面原生SQL语句操作 用DB门面操作的话呢 无非 ...
- PHP制作姓名、学号。爱好等窗口
if (radioButton1.Checked == true) textBox2.Text = 姓名: + textBox1.Text + 性别: + radi ...
- Windows10环境vagrant+VirtualBox虚拟机无法创建私有网络的解决方案。
报错信息 ==> default: Clearing any previously set network interfaces...There was an error while execu ...
- 【BZOJ4817】[Sdoi2017]树点涂色 LCT+线段树
[BZOJ4817][Sdoi2017]树点涂色 Description Bob有一棵n个点的有根树,其中1号点是根节点.Bob在每个点上涂了颜色,并且每个点上的颜色不同.定义一条路径的权值是:这条路 ...
- Strange Optimization(扩展欧几里得)
Strange Optimization Accepted : 67 Submit : 289 Time Limit : 1000 MS Memory Limit : 65536 KB Str ...
- 160822、关于javascrip ==(等号) 和===(恒等)判断
说明 在JavaScript中,下面的值被当做假(false),除了下面列出的值,都被当做真(true): false null undefined 空字符串 数字 0 NaN //属性是代表非数字值 ...
- Object类有哪些公用方法?
Object是所有类的父类,任何类都默认继承Object. clone 保护方法,实现对象的浅复制,只有实现了Cloneable接口才可以调用该方法,否则抛出CloneNotSupportedExce ...
- border inset outset ,border-radius
1. 例子: div{ width:256px; height:256px; border: 10px inset #f00; margin:0 auto; border-radius:255px; ...