`
wangyu070707
  • 浏览: 12603 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
PIpeline读写redis Java使用Pipeline对Redis批量读写(hmset&hgetall)
package com.lxw1234.redis;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Response;


public class Test {
	 
	public static void main(String[] args) throws Exception {
		Jedis redis = new Jedis("127.0.0.1", 6379, 400000);
		Map<String,String> data = new HashMap<String,String>();
		redis.select(8);
		redis.flushDB();
		//hmset 
		long start = System.currentTimeMillis();
		//直接hmset
		for (int i=0;i<10000;i++) {
			data.clear();
			data.put("k_" + i, "v_" + i);
			redis.hmset("key_" + i, data);
		}
		long end = System.currentTimeMillis();
		System.out.println("dbsize:[" + redis.dbSize() + "] .. ");
		System.out.println("hmset without pipeline used [" + (end - start) / 1000 + "] seconds ..");
		redis.select(8);
		redis.flushDB();
		//使用pipeline hmset
		Pipeline p = redis.pipelined();
		start = System.currentTimeMillis();
		for (int i=0;i<10000;i++) {
			data.clear();
			data.put("k_" + i, "v_" + i);
			p.hmset("key_" + i, data);
		}
		p.sync();
		end = System.currentTimeMillis();
		System.out.println("dbsize:[" + redis.dbSize() + "] .. ");
		System.out.println("hmset with pipeline used [" + (end - start) / 1000 + "] seconds ..");
		
		//hmget 
		Set<String> keys = redis.keys("*");
		//直接使用Jedis hgetall
		start = System.currentTimeMillis();
		Map<String,Map<String,String>> result = new HashMap<String,Map<String,String>>();
		for(String key : keys) {
			result.put(key, redis.hgetAll(key));
		}
		end = System.currentTimeMillis();
		System.out.println("result size:[" + result.size() + "] ..");
		System.out.println("hgetAll without pipeline used [" + (end - start) / 1000 + "] seconds ..");
		
		//使用pipeline hgetall
		Map<String,Response<Map<String,String>>> responses = new HashMap<String,Response<Map<String,String>>>(keys.size());
		result.clear();
		start = System.currentTimeMillis();
		for(String key : keys) {
			responses.put(key, p.hgetAll(key));
		}
		p.sync();
		for(String k : responses.keySet()) {
			result.put(k, responses.get(k).get());
		}
		end = System.currentTimeMillis();
		System.out.println("result size:[" + result.size() + "] ..");
		System.out.println("hgetAll with pipeline used [" + (end - start) / 1000 + "] seconds ..");
		
		redis.disconnect();
		
	}
	
	
}
maven 打包压缩静态文件 maven打包压缩css js
	<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.4</version>
				<configuration>
					<!--
						装配过程中排除  不以.min.js -min.js结尾的js文件。
						因为在 yuicompressor配置了不会处理min这些文件所以这些文件要拷贝到对应的目
						录。开始弄的时候就是由于maven-war-plugin会把yuicompressor压缩后的文件覆盖掉,
						所以要做这样的处理,yuicompressor没有压缩的js文件就会拷贝过去,压缩过的js则不会拷贝
						yuicompressor会拷贝压缩后的文件过去
					-->
					<warSourceExcludes>
						%regex[^.+(?:(?&lt;!(?:-|\.)min)\.js)],
						<!-- 这里的这些文件是本地测试用的配置 打包时候不需要 -->
						src/main/resources/config.xml,src/main/resources/jdbc.properties
					</warSourceExcludes>
					<!-- 打包时候需要的config.xml jdbc.properties,它们都在deployFile中。 -->
					<webResources>
						<resource>
							<!-- 这里的目录是相对于 pom.xml来说的 -->
							<directory>deployFile</directory>
							<targetPath>WEB-INF/classes</targetPath>
						</resource>
					</webResources>
				</configuration>
			</plugin>
			<plugin>
				<groupId>net.alchim31.maven</groupId>
				<artifactId>yuicompressor-maven-plugin</artifactId>
				<version>1.3.0</version>
				<executions>
					<execution>
						<phase>process-resources</phase>
						<goals>
							<goal>compress</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
					<!-- 包含js文件和css文件 -->
					<includes>
						<include>**/*.js</include>
						<include>**/*.css</include>
					</includes>
					<!-- 排除本来就压缩的js文件,比如jquery.min.js  xxx-min.js之类的文件 -->
					<excludes>
						<exclude>**/*.min.js</exclude>
						<exclude>**/*-min.js</exclude>
					</excludes>
					<encoding>utf8</encoding>
					<failOnWarning>false</failOnWarning>
					<force>true</force>
					<nosuffix>true</nosuffix>
					<linebreakpos>-1</linebreakpos>
					<sourceDirectory>${basedir}/resources</sourceDirectory>
					<jswarn>false</jswarn>
				</configuration>
			</plugin>
java 获取天气 JAVA获取天气
package javaapplication1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Weather {
	public static String getWeather(String cityCode) {
		String result = "";
		Pattern pattern = Pattern.compile(".*?\"weather\":\"(.*?)\",.*");
		try {
			URL url = new URL("http://www.weather.com.cn/data/cityinfo/" + cityCode + ".html");
			InputStream in = url.openStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8"));
			String line = br.readLine();
			if(null == line) {
				return result;
			} else {
				Matcher matcher = pattern.matcher(line);
				if(matcher.find()) {
					result = matcher.group(1);
				}
			}
			System.out.println(line);
			br.close();
			in.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public static void main(String[] args) {
		System.out.println(Weather.getWeather("101110101"));
	}
}
[简单]poi多sheet分批次导出 [简单]poi多sheet分批次导出
import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
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.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

public class Excel_导出_S01_Test {
	public static void main(String[] args) {
		Excel_导出_S01_Test t = new Excel_导出_S01_Test();
		t.exportExcel("2007", true, 1000, 5000, "f:/saveFile/temp/"+ System.currentTimeMillis() + ".xlsx"); 
		t.exportExcel("2007", false, 1000, 5000, "f:/saveFile/temp/"+System.currentTimeMillis() + ".xlsx");
		t.exportExcel("2007", false, 5000, 1000, "f:/saveFile/temp/"+System.currentTimeMillis() + ".xlsx");
	}

	/**
	 * @Description: 普通的导出excel方法
	 * @param expType
	 *            excel类型2003 2007
	 * @param isDelZero
	 *            是否删除动态列中全部为0的值
	 * @param savePath
	 */
	public void exportExcel(String expType, boolean isDelZero, int pageSize,
			int sheetSize, String savePath) {
		List<String> columnList = new ArrayList<String>();
		// 表头_行数_列数$
		columnList.add("编号#2#1$");
		columnList.add("品牌名#2#1$");
		columnList.add("型号#2#1$");
		StringBuffer cellSb = new StringBuffer();
		cellSb.append("000");
		List<String> dyColumnList = new ArrayList<String>();// 动态列
		int dyNum = 4;
		int subLen = 3;// 子列长度
		for (int i = 1; i < dyNum + 1; i++) {
			// 表头_行数_列数$子列_行数_列数$ 行数固定为1
			dyColumnList.add("下游合作商_" + i + "#1#3$数量#1#1$");
			dyColumnList.add("下游合作商_" + i + "#1#3$金额#1#1$");
			dyColumnList.add("下游合作商_" + i + "#1#3$小计#1#1$");
		}
		dyColumnList.add("合计#1#3$数量合计#1#1$");
		dyColumnList.add("合计#1#3$金额合计#1#1$");
		dyColumnList.add("合计#1#3$小计合计#1#1$");
		String sql = "select 1 from dual";
		Workbook wb = null;
		long resultSize = getSqlResultSize(sql);
		if (isDelZero) {
			if (resultSize * dyColumnList.size() > 65535 * 10L) {
				System.err.println("数据量太大,请使用异步导出");
				return;
			}
			wb = new SXSSFWorkbook(1000);
		} else {
			if ("2003".equals(expType)
					&& columnList.size() + dyColumnList.size() < 256) {
				wb = new HSSFWorkbook();
			} else {
				wb = new SXSSFWorkbook(1000);
			}
		}
		if (resultSize * (columnList.size() + dyColumnList.size()) > 65535 * 15L) {
			System.err.println("数据量太大,请使用异步导出");
			return;
		}
		List<List<Object>> dataList = null;
		Map<Integer, Integer> zeroCols = null;
		//删除0值直接取全部数据,不分sheet页
		if (isDelZero) {
			dataList = getSqlResult(sql, 1, (int) resultSize, dyNum);
			zeroCols = getAllZeroDyColumn(columnList, dyColumnList, dataList,subLen);
		}
		columnList.addAll(dyColumnList);
		if (columnList.size() >= 16383) {
			System.err.println("暂未实现列分割");
			return;
		}else if(columnList.size()>256&&"2003".equals(expType)){
			System.err.println("超出Excel2003限制,选择Excel2007导出");
			wb = new SXSSFWorkbook(1000);
		}
		int startR = 1;
		//动态列求和1合并 0不合并
		for (int p = 0, plen = dyColumnList.size() * 3; p < plen; p++) {
			cellSb.append("1");
		}
		String cellInfo = cellSb.toString();
		boolean isSplitRow = isSplitExcelHead(columnList);// 是否存在动态列
		boolean isCellSum = cellInfo.indexOf("1") > -1;// 是否对列求和
		BigDecimal[] cellSum = new BigDecimal[cellInfo.length()];
		// 初始化
		for (int i = 0; i < cellSum.length; i++) {
			cellSum[i] = new BigDecimal(0);
		}
		final int cellWidth =8;
		final short rowHeight = (short)380;
		// 设置表头数据样式
		CellStyle headStyle = wb.createCellStyle();
		Font headFont = wb.createFont();
		headFont.setBoldweight(Font.BOLDWEIGHT_BOLD);

		headStyle.setFillForegroundColor(IndexedColors.LIME.getIndex());
		headStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
		headStyle.setAlignment(CellStyle.ALIGN_CENTER);
		headStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);

		headStyle.setBorderBottom(CellStyle.BORDER_THIN);
		headStyle.setBorderLeft(CellStyle.BORDER_THIN);
		headStyle.setBorderRight(CellStyle.BORDER_THIN);
		headStyle.setBorderTop(CellStyle.BORDER_THIN);

		headStyle.setTopBorderColor(IndexedColors.LIME.getIndex());
		headStyle.setRightBorderColor(IndexedColors.LIME.getIndex());
		headStyle.setBottomBorderColor(IndexedColors.LIME.getIndex());
		headStyle.setLeftBorderColor(IndexedColors.LIME.getIndex());
		headStyle.setFont(headFont);

		// 普通单元格样式
		CellStyle cellStyle = wb.createCellStyle();
		cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
		cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);

		// 整型样式
		CellStyle longStyle = wb.createCellStyle();
		longStyle.setAlignment(CellStyle.ALIGN_CENTER);
		longStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
		DataFormat format = wb.createDataFormat();
		longStyle.setDataFormat(format.getFormat("####0"));

		// double类型样式
		CellStyle doubleStyle = wb.createCellStyle();
		doubleStyle.setAlignment(CellStyle.ALIGN_CENTER);
		doubleStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
		format = wb.createDataFormat();
		doubleStyle.setDataFormat(format.getFormat("####0.###"));

		// final int pageSize = 502;// 每次取1000条数据
		// final int sheetSize = 101;// 每个sheet大小
		int totalSheet = (int) ((resultSize - 1) / sheetSize + 1);
		boolean isNeedMutil = (sheetSize > pageSize) && (resultSize > pageSize);// 是否需要多次取数据
		if (isDelZero) {
			totalSheet = 1;
		}
		int remainSize = 0, currentRow = 0, lastRow = 0;
		int blen = 0;
		// 分次写满一个sheet页
		for (int st = 1; st <= totalSheet; st++) {
			Sheet sheet = wb.createSheet("报表_" + st);
			sheet.setDefaultColumnWidth(cellWidth);
			sheet.setDefaultRowHeight(rowHeight);
			currentRow = 0;
			blen = 0;
			//写满一个sheet页是否需要多次取值
			isNeedMutil = (sheetSize > pageSize) && (resultSize > pageSize);
			if (isSplitRow) {
				createSheetTitle(sheet, 0, columnList, headStyle);//存在动态列
				startR = 2;
			} else {
				createSheetTitleNormal(sheet, 0, columnList, headStyle);
			}
			System.out.println("-------------------------报表头部写完成--------");
			if (isDelZero) {
				createExcelBody(sheet, startR, dataList, zeroCols, isCellSum,
						true, cellInfo, cellSum, cellStyle, longStyle,doubleStyle);
				dataList.clear();
				dataList=null;
			} else if (!isNeedMutil) {
				//先写上次剩余的数据
				if (remainSize > 0) {
					if (st == totalSheet&& remainSize == resultSize % sheetSize) {
						createExcelSheetBody(sheet,startR,0,dataList,dataList.size() - remainSize,
								(int) Math.min(st * sheetSize, dataList.size()),isCellSum, true, cellInfo, cellSum, cellStyle,longStyle, doubleStyle);
					    dataList.clear();
					    dataList=null;
					    break;
					} else {
						createExcelSheetBody(sheet,startR,0,dataList,dataList.size() - remainSize,
								(int) Math.min(st * sheetSize, dataList.size()),isCellSum, false, cellInfo, cellSum, cellStyle,longStyle, doubleStyle);
					}
					currentRow = remainSize;
					remainSize = Math.max(dataList.size() - st * sheetSize, 0);
					//一次数据填充多个sheet页
					if (remainSize > 0) {
						continue;
					}
					dataList.clear();
					dataList = null;
				}
				int startIndex = remainSize + (st - 1) * sheetSize + 1;// 分页开始字段
				int endIndex = remainSize + st * sheetSize;
				if (endIndex >= resultSize) {
					endIndex = (int) resultSize;
				}
				//System.out.println(startIndex + "----=" + endIndex);
				String pageSql = "select  rptrt.*  from (select rptrow.*, rownum rn from ( "
						+ sql
						+ " ) rptrow  where rownum <="
						+ endIndex
						+ ") rptrt where rn >=" + startIndex;
				dataList = getSqlResult(pageSql, startIndex, endIndex, dyNum);
				if (st == totalSheet) {
					createExcelSheetBody(sheet,startR,currentRow,dataList,0,(int) Math.min(sheetSize - currentRow,
									dataList.size()), isCellSum, true,cellInfo, cellSum, cellStyle, longStyle,doubleStyle);
					dataList.clear();
					dataList=null;
					break;
				} else {
					createExcelSheetBody(sheet,startR,currentRow,dataList,0,(int) Math.min(sheetSize - currentRow,dataList.size()), 
							isCellSum, false,cellInfo, cellSum, cellStyle, longStyle,doubleStyle);
				}
				remainSize = currentRow + dataList.size() - sheetSize;
			} else {
				// 先把剩余的部分写完
				if (remainSize > 0) {
					// 剩余数量最多写一次
					if (st == totalSheet&& remainSize == resultSize % sheetSize) {
						createExcelSheetBody(sheet, startR, 0, dataList,dataList.size() - remainSize,
								Math.min(dataList.size(), sheetSize),
								isCellSum, true, cellInfo, cellSum, cellStyle,
								longStyle, doubleStyle);
						dataList.clear();
						dataList=null;
						break;
					} else {
						createExcelSheetBody(sheet, startR, 0, dataList,
								dataList.size() - remainSize,
								Math.min(dataList.size(), sheetSize),
								isCellSum, false, cellInfo, cellSum, cellStyle,
								longStyle, doubleStyle);
					}
					dataList.clear();
					dataList = null;
					currentRow = remainSize;
				}
				if (resultSize - (st - 1) * sheetSize - 1 - currentRow < 0) {
					continue;
				} else {
					blen = (int) Math.min((sheetSize - 1) / pageSize + 1, (resultSize- (st - 1) * sheetSize - 1 - currentRow)
									/ pageSize + 1);
				}
				//分次取数据
				for (int b = 0; b < blen; b++) {
					if (!isNeedMutil) {
						break;
					}
					lastRow = currentRow;
					int startIndex = remainSize + (st - 1) * sheetSize + 1 + b
							* pageSize;// 分页开始字段
					int endIndex = remainSize + (st - 1) * sheetSize + (b + 1)
							* pageSize;
					if (endIndex >= resultSize) {
						endIndex = (int) resultSize;
						isNeedMutil = false;
						remainSize = 0;
					}
					if (endIndex > st * sheetSize) {
						remainSize = endIndex - st * sheetSize;
						currentRow = st * sheetSize + 1;
						isNeedMutil = false;
					}
					//System.out.println(startIndex + "---=" + endIndex + "---="+ lastRow);
					String pageSql = "select  rptrt.*  from (select rptrow.*, rownum rn from ( "
							+ sql
							+ " ) rptrow  where rownum <="
							+ endIndex
							+ ") rptrt where rn >=" + startIndex;
					dataList = getSqlResult(pageSql, startIndex, endIndex,
							dyNum);
					if (st == totalSheet&&endIndex==resultSize) {
						createExcelSheetBody(sheet, startR, lastRow, dataList,0, dataList.size() - remainSize, 
								isCellSum,true, cellInfo, cellSum, cellStyle, longStyle,doubleStyle);
						dataList.clear();
						dataList=null;
						break;
					} else {
						createExcelSheetBody(sheet, startR, lastRow, dataList,0, dataList.size() - remainSize,
								isCellSum,false, cellInfo, cellSum, cellStyle, longStyle,doubleStyle);
					}
					currentRow += dataList.size() - remainSize;
				}
			}
		}
		System.out.println("------------报表保存----");
		writeExcel(wb, savePath);
	}

	public void createExcelSheetBody(Sheet sheet, int headSize, int startR,
			List bodyList, int startIndex, int endIndex, boolean isCellSum,
			boolean isLast, String cellInfo, BigDecimal[] cellSum,
			CellStyle cellStyle, CellStyle longStyle, CellStyle doubleStyle) {
		Row rowBody = null;
		Cell cell = null;
		if (bodyList.size() > 0) {
			List rowList = null;
			int colEnd = ((List) bodyList.get(0)).size();
			int rowEnd = bodyList.size();// TODO
			for (int i = startIndex, idx = 0; i < endIndex; i++, idx++) {
				rowBody = sheet.createRow(idx + headSize + startR);
				rowList = (List) bodyList.get(i);
				if (!isCellSum) {
					for (int j = 0; j < colEnd; j++) {
						cell = rowBody.createCell(j);
						Object obj = rowList.get(j);
						// double long string判断
						try {
							double var = Double.parseDouble(obj + "");
							cell.setCellValue(var);
							if (var != (long) var) {
								cell.setCellStyle(doubleStyle);
							} else {
								cell.setCellStyle(longStyle);
							}
						} catch (Exception e) {
							cell.setCellValue(obj + "");
							cell.setCellStyle(cellStyle);
						}
					}
				} else {
					for (int j = 0; j < colEnd; j++) {
						cell = rowBody.createCell(j);
						Object obj = rowList.get(j);
						try {
							double var = Double.parseDouble(obj + "");
							cell.setCellValue(var);
							if (var != (long) var) {
								cell.setCellStyle(doubleStyle);
							} else {
								cell.setCellStyle(longStyle);
							}
							if (cellInfo.charAt(j) == '1') {
								cellSum[j] = cellSum[j]
										.add(new BigDecimal(var));
							}
						} catch (Exception e) {
							cell.setCellValue(obj + "");
							cell.setCellStyle(cellStyle);
						}
					}
				}
				rowList = null;
			}
			if (isCellSum) {
				if (isLast) {
					rowBody = sheet.createRow(headSize + startR + endIndex
							- startIndex);
					cell = rowBody.createCell(0);
					cell.setCellStyle(doubleStyle);
					cell.setCellValue("合计");
					for (int j = 1; j < colEnd; j++) {
						if (cellInfo.charAt(j) == '1') {
							cell = rowBody.createCell(j);
							cell.setCellStyle(doubleStyle);
							cell.setCellValue(cellSum[j]
									.setScale(3, BigDecimal.ROUND_HALF_UP)
									.stripTrailingZeros().toPlainString());
						}
					}
				}
			}
		}
		rowBody = null;
		cell = null;
	}

	/**
	 * @Description: 得到List中0值位置
	 */
	public Map<Integer, Integer> getAllZeroDyColumn(List<String> columnList,
			List<String> dyList, List<List<Object>> dataList, int subLen) {
		Map<Integer, Integer> nullIndexMap = new HashMap<Integer, Integer>();
		int startIndex = columnList.size();
		List<Object> list = null;
		boolean isNull = true;
		list = dataList.get(0);
		// 得到第一行数据所有为0的索引位置
		for (int i = startIndex, len = list.size(); i < len; i += subLen) {
			isNull = true;
			for (int k = i, klen = i + subLen; k < klen && k < len; k++) {
				if (!isZero(list.get(k).toString())) {
					isNull = false;
					break;
				}
			}
			if (isNull) {
				nullIndexMap.put((i - startIndex) / subLen, 1);
			}
		}
		for (int j = 1, len2 = dataList.size(); j < len2; j++) {
			list = dataList.get(j);
			for (int i = startIndex, len = list.size(); i < len; i += subLen) {
				isNull = true;
				if (nullIndexMap.get((i - startIndex) / subLen) != null) {
					for (int k = i, klen = i + subLen; k < klen && k < len; k++) {
						if (!isZero(list.get(k).toString())) {
							isNull = false;
							break;
						}
					}
					if (!isNull) {
						// 逐步缩小为0的索引值
						nullIndexMap.remove((i - startIndex) / subLen);
					}
				}
			}
			list = null;
		}
		List<String> dyList2 = new ArrayList<String>();
		Map<Integer, Integer> resultMap = new HashMap<Integer, Integer>();
		for (Entry<Integer, Integer> entry : nullIndexMap.entrySet()) {
			for (int k = 0; k < subLen; k++) {
				resultMap.put(entry.getKey() * subLen + startIndex + k, subLen);
				dyList2.add(dyList.get(entry.getKey() * subLen + k));
			}
		}
		dyList.removeAll(dyList2);
		dyList = new ArrayList(dyList);
		dyList2.clear();
		dyList2 = null;
		list = null;
		return resultMap;
	}

	/**
	 * @Description: 是否为0
	 */
	public boolean isZero(String str) {
		return str == null || str.replaceAll("(0|,|\\.)", "").length() == 0;
	}

	private List<List<Object>> getSqlResult(String pageSql, int startIndex,
			int endIndex, int dyNum) {
		return generateTestData(startIndex, endIndex, dyNum);
	}

	// 产生随机数据
	public List<List<Object>> generateTestData(int startIndex, int totalNum,
			int dyNum) {
		Random random = new Random(System.nanoTime());
		List<List<Object>> tmpDataList = new ArrayList<List<Object>>();
		List<Object> dataList = null;
		// 产生随机为0的列
		List<Integer> tmpList = new ArrayList<Integer>();
		for (int i = 0, len = dyNum * 3 / 4; i < len; i++) {
			tmpList.add(random.nextInt(dyNum) % (dyNum - 1));
		}
		random = new Random(System.nanoTime());
		// 去重
		Set<Integer> nullValue = new TreeSet(tmpList);
		System.out.println("----重复数据-----");
		for (Integer it : nullValue) {
			System.out.print((3 + it * 3) + ",");
		}
		System.out.println();
		for (int i = startIndex; i <= totalNum; i++) {
			dataList = new ArrayList<Object>();
			dataList.add(i);
			dataList.add("测试");
			dataList.add("数据");
			for (int k = 0; k < dyNum + 1; k++) {
				if (nullValue.contains(k)) {
					dataList.add(String.valueOf(0));
					dataList.add(String.valueOf(0));
					dataList.add(String.valueOf(0));
				} else {
					dataList.add(String.valueOf(Math.abs(random.nextLong()) % 4000 * 0.098));
					dataList.add(String.valueOf(Math.abs(random.nextInt() % 100)));
					dataList.add(String.valueOf(Math.abs(random.nextInt() % 10000)));
				}
			}
			tmpDataList.add(dataList);
			dataList = null;
		}
		return tmpDataList;
	}

	public long getSqlResultSize(String sql) {
		return 10000;
	}

	/**
	 * @Description: 是否存在动态列
	 */
	public boolean isSplitExcelHead(List<String> headList) {
		for (String str : headList) {
			if (str.indexOf("#") > -1 && str.indexOf("$") > -1) {
				return true;
			}
		}
		return false;
	}

	public  void writeExcel(Workbook wb, String fileName) {
		if (wb != null) {
			try {
				FileOutputStream fileOutStream = new FileOutputStream(fileName);
				wb.write(fileOutStream);
				if (fileOutStream != null) {
					fileOutStream.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * @Description:删除0值
	 * @param startR
	 *            开始行
	 * @param bodyList
	 *            数据
	 * @param isCellSum
	 *            是否求列和
	 * @param isLast
	 *            是否是最后一个Sheet页
	 * @param cellInfo
	 *            列求和信息1求和0不求和
	 * @param cellSum
	 *            列求和数组
	 */
	public void createExcelBody(Sheet sheet, int startR, List bodyList,
			Map<Integer, Integer> zeroCols, boolean isCellSum, boolean isLast,
			String cellInfo, BigDecimal[] cellSum, CellStyle cellStyle,
			CellStyle longStyle, CellStyle doubleStyle) {
		Row rowBody = null;
		Cell cell = null;
		if (bodyList.size() > 0) {
			List rowList = null;
			int rowEnd = bodyList.size() + startR;
			int colEnd = ((List) bodyList.get(0)).size();
			for (int i = startR, idx = 0; i < rowEnd; i++, idx++) {
				rowBody = sheet.createRow(i);
				rowList = (List) bodyList.get(idx);
				if (!isCellSum) {
					for (int j = 0, col = 0; j < colEnd; j++) {
						if (zeroCols.containsKey(j)) {
							j += zeroCols.get(j) - 1;
							continue;
						}
						cell = rowBody.createCell(col);
						Object obj = rowList.get(j);
						// double long string判断
						try {
							double var = Double.parseDouble(obj + "");
							cell.setCellValue(var);
							if (var != (long) var) {
								cell.setCellStyle(doubleStyle);
							} else {
								cell.setCellStyle(longStyle);
							}
						} catch (Exception e) {
							cell.setCellValue(obj + "");
							cell.setCellStyle(cellStyle);
						}
						col++;
					}
				} else {
					for (int j = 0, col = 0; j < colEnd; j++) {
						if (zeroCols.containsKey(j)) {
							j += zeroCols.get(j) - 1;
							continue;
						}
						cell = rowBody.createCell(col);
						Object obj = rowList.get(j);
						try {
							double var = Double.parseDouble(obj + "");
							cell.setCellValue(var);
							if (var != (long) var) {
								cell.setCellStyle(doubleStyle);
							} else {
								cell.setCellStyle(longStyle);
							}
							if (cellInfo.charAt(col) == '1') {
								cellSum[col] = cellSum[col].add(new BigDecimal(
										var));
							}
						} catch (Exception e) {
							cell.setCellValue(obj + "");
							cell.setCellStyle(cellStyle);
						}
						col++;
					}
				}
				rowList = null;
			}
			if (isCellSum) {
				if (isLast) {
					rowBody = sheet.createRow(rowEnd);
					cell = rowBody.createCell(0);
					cell.setCellStyle(doubleStyle);
					cell.setCellValue("合计");
					for (int j = 1, col = 1; j < colEnd; j++) {
						if (zeroCols.containsKey(j)) {
							j += zeroCols.get(j) - 1;
							continue;
						}
						if (cellInfo.charAt(col) == '1') {
							cell = rowBody.createCell(col);
							cell.setCellStyle(doubleStyle);
							cell.setCellValue(cellSum[col]
									.setScale(3, BigDecimal.ROUND_HALF_UP)
									.stripTrailingZeros().toPlainString());
						}
						col++;
					}
				}
			}
		}
		rowBody = null;
		cell = null;
	}
	
	public  void createSheetTitleNormal(Sheet sheet, int startR,
			List<String> headList, CellStyle headStyle) {
		Row row_head = sheet.createRow(startR);
		Cell cell = null;
		int start_col = 0;// 起始列
		String headStr = null;
		for (int i = 0; i < headList.size(); i++) {
			headStr = String.valueOf(headList.get(i));
			cell = row_head.createCell(start_col + i);
			cell.setCellValue(headStr);
			cell.setCellStyle(headStyle);
		}
	}

	/**
	 * @Description: Excel 表头为固定2行
	 */
	public  void createSheetTitle(Sheet sheet, int startR,
			List<String> headList, CellStyle headStyle) {
		Row row_head_0 = null;
		Row row_head_1 = null;
		Cell cell = null;
		// 保存未拆分时的值
		String attr = null;
		// 保存第一次拆分时的值
		String[] tmp = null;
		// 拆分后第一行的值
		String[] tmp2 = null;
		// 拆分后第2行的值
		String[] tmp3 = null;
		// 第一次匹配时的值
		String preAttr = null;
		// 下一次匹配时的值
		String lastAttr = null;
		int preIdx = 0;
		int start_row = startR;// 起始行
		int start_col = 0;// 起始列
		row_head_0 = sheet.createRow(start_row + 0);
		row_head_1 = sheet.createRow(start_row + 1);
		CellRangeAddress range = new CellRangeAddress(0, 0, 0, 0);
		for (int i = 0; i < headList.size(); i++) {
			attr = String.valueOf(headList.get(i));
			// 以$分割数据
			tmp = attr.split("\\$");
			if (tmp.length == 1) {
				cell = row_head_0.createCell(start_col + i);
				// #分割的是行 列信息
				cell.setCellValue(tmp[0].split("\\#")[0]);
				cell.setCellStyle(headStyle);
				cell = row_head_1.createCell(start_col + i);
				cell.setCellStyle(headStyle);
				// 创建cell主要是指定样式,防止跨列时少样式
				range.setFirstRow(start_row + 0);
				range.setLastRow(start_row + 1);
				range.setFirstColumn(start_col + i);
				range.setLastColumn(start_col + i);
				sheet.addMergedRegion(range);
				preIdx = i + 1;
			} else if (tmp.length == 2) {
				preAttr = tmp[0];
				tmp2 = tmp[0].split("\\#");
				tmp3 = tmp[1].split("\\#");
				int tmpI = Integer.parseInt(tmp2[2]);// 跨列
				int tmpI2 = Integer.parseInt(tmp3[2]);// 跨列信息
				if (!preAttr.equals(lastAttr)) {
					lastAttr = tmp[0];
					cell = row_head_0.createCell(start_col + i);
					cell.setCellValue(tmp2[0]);
					cell.setCellStyle(headStyle);
					range.setFirstRow(start_row + 0);
					range.setLastRow(start_row + 0);
					range.setFirstColumn(start_col + preIdx);
					range.setLastColumn(start_col + preIdx + tmpI - 1);
					sheet.addMergedRegion(range);
				}
				cell = row_head_1.createCell(start_col + i);
				cell.setCellValue(tmp3[0]);
				cell.setCellStyle(headStyle);
				// 第二行跨列
				if (tmpI2 != 1) {
					range.setFirstRow(start_row + 1);
					range.setLastRow(start_row + 1);
					range.setFirstColumn(start_col + preIdx);
					range.setLastColumn(start_col + preIdx + tmpI2 - 1);
					sheet.getRow(start_row + 1)
							.createCell(start_col + preIdx + tmpI2 - 1)
							.setCellStyle(headStyle);
					sheet.addMergedRegion(range);
					start_col = start_col + tmpI2 - 1;
				}
				preIdx++;
			}
		}
	}

}
JXL做报表 jxl 做的报表通用方法
ExcelUtilToolTest.java
Global site tag (gtag.js) - Google Analytics