⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exceltools.java

📁 很好的java excle组件
💻 JAVA
字号:
package com.tools;

import java.util.ArrayList;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;

public class ExcelTools implements ExcelInterface {

	private static ExcelTools instance = null;

	private ExcelTools() {
	}

	public synchronized static ExcelTools getInstance() {

		if (null == instance) {
			instance = new ExcelTools();
		}
		return instance;
	}

	public HSSFSheet createSheet(HSSFWorkbook workbook, int sheetIndex,
			String sheetName) {
		HSSFSheet sheet = workbook.createSheet();
		workbook.setSheetName(sheetIndex, sheetName);
		return sheet;
	}

	public HSSFWorkbook createWorkBook() {

		return new HSSFWorkbook();
	}

	public void fillCellData(HSSFSheet sheet,
			ArrayList<Map<String, String>> data, ArrayList<String> colNameList,
			int rowIndex) {
		
		int rowCount = data.size();
		System.out.println("总行数:"+rowCount);
		for (int i = 0; i < rowCount; i++) {
			int fillRowIndex = rowIndex + i;
			HSSFRow row = sheet.getRow(fillRowIndex);
			if (null == row) {
				row = sheet.createRow(fillRowIndex);
			} else {
				sheet.removeRow(row);
				row = sheet.createRow(fillRowIndex);
			}

			for (int j = 0; j < colNameList.size(); j++) {
				Map<String, String> map = data.get(i);
				String colName = colNameList.get(j);
				String cellValue = map.get(colName);
				HSSFCell cell = row.createCell((short) j);
				cell.setCellValue(cellValue);
			}
		}
	}

	public void fillColName(HSSFSheet sheet, ArrayList<String> colNameList,
			int rowIndex) {
		HSSFRow row = sheet.createRow(rowIndex);
		int colCount = colNameList.size();
		System.out.println("列数:"+colCount);
		for (int i = 0; i < colCount; i++) {
			String colName = colNameList.get(i);
			HSSFCell cell = row.createCell((short) i);
			cell.setCellType(HSSFCell.CELL_TYPE_STRING);
			cell.setCellValue(colName);
		}
	}

	public void fillTitle(HSSFSheet sheet, String title, int rowIndex,
			short colIndex) {
		HSSFRow row = sheet.createRow(rowIndex);
		row.setHeight((short)20);
		HSSFCell cell = row.createCell(colIndex);
		cell.setCellType(HSSFCell.CELL_TYPE_STRING);
		cell.setCellValue(title);

	}

	public void mergeCell(HSSFSheet sheet, int rowFrom, short colFrom,
			int rowTo, short colTo) {

		Region region = new Region(rowFrom, colFrom, rowTo, colTo);
		sheet.addMergedRegion(region);
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -