excel로 데이터 내보내기

2287 단어 excel
자세히 보기
/**
	 * 
	 * @param datas	 
	 * @param filePath	 
	 * @param title	 
	 * @return
	 */
	public boolean ListArray2Excel(List datas, String filePath,
			String[] title) {
		boolean flag = false;
		WritableWorkbook workbook = null;
		WritableSheet sheet = null;
		Label label = null;

		//  Excel 
		try {
			workbook = Workbook.createWorkbook(new File(filePath));

			//  Excel sheet
			sheet = workbook.createSheet("seo_url", 0);

			//  Excel 

			int row = 0;

			//  
			for (int i = 0; i < title.length; i++) {
				label = new Label(i, row, title[i]);
				sheet.addCell(label);
			}

			row++;

			for (String[] data : datas) {
				for (int i = 0; i < data.length; i++) {
					for (int j = 0; j < title.length; j++) {
						label = new Label(i, row, data[i]);
						sheet.addCell(label);

					}
				}
				row++;
			}

			//  
			workbook.write();
			workbook.close();
			workbook = null;
			flag = true;

		} catch (RowsExceededException e) {

		} catch (WriteException e) {

		} catch (IOException e) {

		} finally {
			try {
				workbook.close();
			} catch (Exception e) {
			}
		}
		return flag;
	}

테스트:
/**
	 *  
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		//
		List data = new ArrayList();
		String[] strArray = { " ", "http://www.baidu.com" };
		String[] strArray2 = { " ", "http://www.guge.com" };
		data.add(strArray);
		data.add(strArray2);

		try {

			String[] title = { " ", "url" };
			if (!new ExcelUtil().ListArray2Excel(data,
					"d:/test/" + System.currentTimeMillis() + ".xls", title)) {
				System.out.println(" ");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

좋은 웹페이지 즐겨찾기