`
dreamoftch
  • 浏览: 486551 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

导出excel

 
阅读更多
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

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.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;


private void doExport() {
		Object[] options = { "只导出当前页数据", "导出所有数据" };
		Page<WmsWebsiteStockDifference> page = null;
		List<WmsWebsiteStockDifference> wmsWebsiteStockDifferenceList = null;
		StockLocationAction stockLocationAction = null;
		// 导出文件的路径,从对话框里获得后,仅有要保存的文件名,没有.xls的扩展名
		String filePath = null;
		// 用户输入的文件名
		String fileName = null;
		File file = null;
		int odoHeadListSize = 0;
		int checkResult = 0;
		int type = JOptionPane.showOptionDialog(null, "请选择导出数据范围!", "请选择",	JOptionPane.YES_OPTION, JOptionPane.QUESTION_MESSAGE, null,
				options, options[0]);
		if (-1 == type) {
			// 关闭询问对话框时返回
			return;
		}
		// 保存文件对话框
		JFileChooser jfc = new JFileChooser();
		jfc.setFileFilter(new ExcelFilter());
		jfc.setDialogTitle("保存文件");
		jfc.showSaveDialog(XXX);
		if (null == jfc.getSelectedFile()) {
			return;
		} else {
			filePath = jfc.getSelectedFile().getAbsolutePath().substring(0,jfc.getSelectedFile().getAbsolutePath().lastIndexOf(File.separator) + 1);
			fileName = jfc.getSelectedFile().getAbsolutePath().substring(jfc.getSelectedFile().getAbsolutePath().lastIndexOf(File.separator) + 1,
							jfc.getSelectedFile().getAbsolutePath().length());
			checkResult = CheckSameExportName.checkExcelName(filePath+ fileName);
			// 加入对选择文件的判断 
			if (CheckSameExportName.DO_NOTHING == checkResult) {
				return;
			} else if (CheckSameExportName.CHANGE_NAME == checkResult) {
				fileName = fileName.replaceAll(".xls", "").concat("_").concat(CheckSameExportName.getFileNameSuffix());
			}
		}
		try {
			if (0 == type) {
				// 1. 导出当前页,从list界面获取数据
				page = listPanel.getPage();
				if (null == page) {
					JOptionPane.showMessageDialog(null, "没有要导出的数据!");
					return;
				} else {
					wmsWebsiteStockDifferenceList = page.getResult();
					if (null == wmsWebsiteStockDifferenceList|| wmsWebsiteStockDifferenceList.isEmpty()) {
						JOptionPane.showMessageDialog(null, "没有要导出的商品!");
						return;
					} else {
						doExportAction(wmsWebsiteStockDifferenceList, filePath + File.separator + fileName + ".xls");
					}
				}
			} else {
				// 2. 导出所有数据,从库里查
				try {
					// 获取当前查询条件,以便导出。
					ConditionMap condition = new ConditionMap();
					condition.setCondition(listPanel.getConditionMap()
							.getCondition());
					condition.setHql(listPanel.getConditionMap().getHql());
					// 判断当前页面有没有内容
					if (null != listPanel.getPage()) {
						int pageNumber = (int) listPanel.getPage()
								.getTotalCount() / 5000 + 1;
						condition.setPageSize(5000);
						condition.setPageNo(1);
						stockLocationAction = new StockLocationAction();
						Object o = stockLocationAction.getWmsWebsiteStockDifference(condition);
						if (o instanceof Page<?>) {
							page = (Page<WmsWebsiteStockDifference>) o;
							wmsWebsiteStockDifferenceList = page.getResult();
						} else {
							return;
						}
						odoHeadListSize = wmsWebsiteStockDifferenceList.size();
						if (odoHeadListSize < excelPageSize) {
							doExportAction(wmsWebsiteStockDifferenceList, filePath
									+ File.separator + fileName + ".xls");
						} else {
							// 按5k一个excel拆分成若干文件,导出,先创建文件夹,将导出内容全放在文件夹里
							filePath = filePath + "仓库网站库存差" + DateHelper.formatToDay(Calendar.getInstance().getTime()).replaceAll("-", "");
							file = new File(filePath);
							if (!file.exists()) {
								file.mkdirs();
							}
							for (int p = 1; p <= pageNumber; p++) {
								try {
									condition.setPageNo(p);
						 			stockLocationAction = new StockLocationAction();
									o = stockLocationAction.getWmsWebsiteStockDifference(condition);
									if (o instanceof Page<?>) {
										page = (Page<WmsWebsiteStockDifference>) o;
										wmsWebsiteStockDifferenceList = page.getResult();
									} else {
										return;
									}
									if (null == wmsWebsiteStockDifferenceList|| wmsWebsiteStockDifferenceList.isEmpty()) {
										JOptionPane.showMessageDialog(null,"没有要导出的商品!");
										return;
									}

								} catch (Exception e) {
									JExceptionDialog.traceException(XXX,"导出出现异常! cause: " + e.getMessage(),e);
									e.printStackTrace();
									return;
								}
								try {
									doExportAction(
											wmsWebsiteStockDifferenceList,	filePath + File.separator	+ fileName + "_"	+ String.valueOf(p) + ".xls");
								} catch (Exception e) {
									JExceptionDialog.traceException(XXX.getMainFrame(),"导出出现异常! cause: " + e.getMessage(),	e);
									e.printStackTrace();
									return;
								}
							}
						}
					}
				} catch (Exception e) {
					JExceptionDialog.traceException(XXX, "导出出现异常! cause: " + e.getMessage(), e);
					e.printStackTrace();
					return;
				}
			}
		} catch (Exception e) {
			JExceptionDialog.traceException(XXX, "导出出现异常! cause: " + e.getMessage(), e);
			e.printStackTrace();
			return;
		}

	}

	/**
	 * 
	 * Created on 2013-2-17
	 * <p>
	 * Discription:[导出excel]
	 * </p>
	 * 
	 * @author:[田超辉] [tianchaohui]@homevv.com
	 * @update:[日期YYYY-MM-DD] [更改人姓名] void
	 * @param pageContent
	 */
	private void doExportAction(
			List<WmsWebsiteStockDifference> wmsWebsiteStockDifferenceList,
			String fileName) {
		File file = new File(fileName);
		if (listPanel.getPage() != null) {
			HSSFWorkbook hwb = new HSSFWorkbook();
			HSSFCell hc = null;
			HSSFRow hr = null;
			HSSFSheet hs = null;
			FileOutputStream fileOutputStream = null;
			HSSFCellStyle cellStyle = hwb.createCellStyle();
			HSSFFont cellFont = hwb.createFont();
			cellStyle.setFont(cellFont);
			hs = hwb.createSheet();
			hr = hs.createRow((short) 0);
			try {
				fileOutputStream = new FileOutputStream(file);
				hc = hr.createCell(0);
				hc.setCellValue("商品编号");
				hc = hr.createCell(1);
				hc.setCellValue("商品名称");
				hc = hr.createCell(2);
				hc.setCellValue("仓库库存");
				hc = hr.createCell(3);
				hc.setCellValue("仓库库存占用数");
				hc = hr.createCell(4);
				hc.setCellValue("网站库存");
				hc = hr.createCell(5);
				hc.setCellValue("网站库存占用数");
				hc = hr.createCell(6);
				hc.setCellValue("库存差");
				for (WmsWebsiteStockDifference wmsWebsiteStockDifference : wmsWebsiteStockDifferenceList) {
					hr = hs.createRow((hs.getLastRowNum() + 1));
					hc = hr.createCell(0);
					hc.setCellValue((wmsWebsiteStockDifference.getProductNo() != null) ? wmsWebsiteStockDifference	.getProductNo() : "");
					hc = hr.createCell(1);
					hc.setCellValue((wmsWebsiteStockDifference.getProductName() != null && !wmsWebsiteStockDifference.getProductName().equals("null")) ? wmsWebsiteStockDifference
							.getProductName() : "");
					hc = hr.createCell(2);
					hc.setCellValue((wmsWebsiteStockDifference.getWmsStock() != null) ? wmsWebsiteStockDifference.getWmsStock().toString() : "0");
					hc = hr.createCell(3);
					hc.setCellValue((wmsWebsiteStockDifference.getWmsDistrubuteStock() != null) ? wmsWebsiteStockDifference.getWmsDistrubuteStock().toString() : "0");
					hc = hr.createCell(4);
					hc.setCellValue((wmsWebsiteStockDifference.getWebsiteStock() != null) ? wmsWebsiteStockDifference.getWebsiteStock().toString() : "0");
					hc = hr.createCell(5);
					hc.setCellValue((wmsWebsiteStockDifference.getWebSiteDistrubuteStock() != null) ? wmsWebsiteStockDifference.getWebSiteDistrubuteStock().toString() : "0");
					hc = hr.createCell(6);
					hc.setCellValue((wmsWebsiteStockDifference.getWmsWebsiteStockDifference() != null) ? wmsWebsiteStockDifference.getWmsWebsiteStockDifference().toString() : "0");
				}
				hwb.write(fileOutputStream);
				JOptionPane.showMessageDialog(null, "文件导出成功");
				Runtime.getRuntime().exec("cmd /c start \"\" \"" + file + "\"");
			} catch (Exception e) {
				e.printStackTrace();
				JExceptionDialog.traceException(XXX, e.getMessage(), e.getMessage(), e);
			} finally {
				try {
					if (fileOutputStream != null)
						fileOutputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
					JExceptionDialog.traceException(XXX, e.getMessage(), e.getMessage(), e);
				}
			}
		} else {
			JOptionPane.showMessageDialog(null, "没有记录!");
		}
	}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics