Java Code Examples for org.apache.poi.ss.usermodel.Workbook#setSheetName()

The following examples show how to use org.apache.poi.ss.usermodel.Workbook#setSheetName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExcelExportOfTemplateUtil.java    From autopoi with Apache License 2.0 5 votes vote down vote up
public Workbook createExcleByTemplate(TemplateExportParams params, Class<?> pojoClass, Collection<?> dataSet, Map<String, Object> map) {
	// step 1. 判断模板的地址
	if (params == null || map == null || StringUtils.isEmpty(params.getTemplateUrl())) {
		throw new ExcelExportException(ExcelExportEnum.PARAMETER_ERROR);
	}
	Workbook wb = null;
	// step 2. 判断模板的Excel类型,解析模板
	try {
		this.teplateParams = params;
		wb = getCloneWorkBook();
		// 创建表格样式
		setExcelExportStyler((IExcelExportStyler) teplateParams.getStyle().getConstructor(Workbook.class).newInstance(wb));
		// step 3. 解析模板
		for (int i = 0, le = params.isScanAllsheet() ? wb.getNumberOfSheets() : params.getSheetNum().length; i < le; i++) {
			if (params.getSheetName() != null && params.getSheetName().length > i && StringUtils.isNotEmpty(params.getSheetName()[i])) {
				wb.setSheetName(i, params.getSheetName()[i]);
			}
			tempCreateCellSet.clear();
			parseTemplate(wb.getSheetAt(i), map);
		}
		if (dataSet != null) {
			// step 4. 正常的数据填充
			dataHanlder = params.getDataHanlder();
			if (dataHanlder != null) {
				needHanlderList = Arrays.asList(dataHanlder.getNeedHandlerFields());
			}
			addDataToSheet(pojoClass, dataSet, wb.getSheetAt(params.getDataSheetNum()), wb);
		}
	} catch (Exception e) {
		LOGGER.error(e.getMessage(), e);
		return null;
	}
	return wb;
}
 
Example 2
Source File: ExcelExportOfTemplateUtil.java    From jeasypoi with Apache License 2.0 5 votes vote down vote up
public Workbook createExcleByTemplate(TemplateExportParams params, Class<?> pojoClass, Collection<?> dataSet, Map<String, Object> map) {
	// step 1. 判断模板的地址
	if (params == null || map == null || StringUtils.isEmpty(params.getTemplateUrl())) {
		throw new ExcelExportException(ExcelExportEnum.PARAMETER_ERROR);
	}
	Workbook wb = null;
	// step 2. 判断模板的Excel类型,解析模板
	try {
		this.teplateParams = params;
		wb = getCloneWorkBook();
		// 创建表格样式
		setExcelExportStyler((IExcelExportStyler) teplateParams.getStyle().getConstructor(Workbook.class).newInstance(wb));
		// step 3. 解析模板
		for (int i = 0, le = params.isScanAllsheet() ? wb.getNumberOfSheets() : params.getSheetNum().length; i < le; i++) {
			if (params.getSheetName() != null && params.getSheetName().length > i && StringUtils.isNotEmpty(params.getSheetName()[i])) {
				wb.setSheetName(i, params.getSheetName()[i]);
			}
			tempCreateCellSet.clear();
			parseTemplate(wb.getSheetAt(i), map);
		}
		if (dataSet != null) {
			// step 4. 正常的数据填充
			dataHanlder = params.getDataHanlder();
			if (dataHanlder != null) {
				needHanlderList = Arrays.asList(dataHanlder.getNeedHandlerFields());
			}
			addDataToSheet(pojoClass, dataSet, wb.getSheetAt(params.getDataSheetNum()), wb);
		}
	} catch (Exception e) {
		LOGGER.error(e.getMessage(), e);
		return null;
	}
	return wb;
}
 
Example 3
Source File: UserCSVUploadGet.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
  protected void populateBody(Object resource, Workbook workbook, Sheet sheet,
	       List<QName> properties) throws IOException {
      // Set the sheet name
      workbook.setSheetName(0, "UsersToAdd");

      // Add a blank line
      sheet.createRow(1);
}
 
Example 4
Source File: ExcelExportOfTemplateUtil.java    From easypoi with Apache License 2.0 5 votes vote down vote up
public Workbook createExcleByTemplate(TemplateExportParams params, Class<?> pojoClass,
                                      Collection<?> dataSet, Map<String, Object> map) {
    // step 1. 判断模板的地址
    if (params == null || map == null || StringUtils.isEmpty(params.getTemplateUrl())) {
        throw new ExcelExportException(ExcelExportEnum.PARAMETER_ERROR);
    }
    Workbook wb = null;
    // step 2. 判断模板的Excel类型,解析模板
    try {
        this.teplateParams = params;
        wb = getCloneWorkBook();
        // 创建表格样式
        setExcelExportStyler((IExcelExportStyler) teplateParams.getStyle()
            .getConstructor(Workbook.class).newInstance(wb));
        // step 3. 解析模板
        for (int i = 0, le = params.isScanAllsheet() ? wb.getNumberOfSheets() : params
            .getSheetNum().length; i < le; i++) {
            if (params.getSheetName() != null && params.getSheetName().length > i
                && StringUtils.isNotEmpty(params.getSheetName()[i])) {
                wb.setSheetName(i, params.getSheetName()[i]);
            }
            tempCreateCellSet.clear();
            parseTemplate(wb.getSheetAt(i), map);
        }
        if (dataSet != null) {
            // step 4. 正常的数据填充
            dataHanlder = params.getDataHanlder();
            if (dataHanlder != null) {
                needHanlderList = Arrays.asList(dataHanlder.getNeedHandlerFields());
            }
            addDataToSheet(pojoClass, dataSet, wb.getSheetAt(params.getDataSheetNum()), wb);
        }
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
        return null;
    }
    return wb;
}
 
Example 5
Source File: ExcelExportOfTemplateUtil.java    From jeewx with Apache License 2.0 5 votes vote down vote up
private static Workbook createSheetInUserModel2FileByTemplate(
		TemplateExportParams params, Class<?> pojoClass,
		Collection<?> dataSet, Map<String, Object> map) {
	// step 1. 判断模板的地址
	if (StringUtils.isEmpty(params.getTemplateUrl())) {
		return null;
	}
	Workbook wb = null;
	// step 2. 判断模板的Excel类型,解析模板
	try {
		wb = getCloneWorkBook(params);
		// step 3. 删除其他的sheet
		for (int i = wb.getNumberOfSheets()-1; i >= 0; i--) {
			if (i != params.getSheetNum()) {
				wb.removeSheetAt(i);
			}
		}
		if(StringUtils.isNotEmpty(params.getSheetName())){
			wb.setSheetName(0, params.getSheetName());
		}
		// step 4. 解析模板
		parseTemplate(wb.getSheetAt(0), map);
		if(dataSet != null){
			// step 5. 正常的数据填充
			addDataToSheet(params, pojoClass, dataSet, wb.getSheetAt(0), wb);
		}
	} catch (Exception e) {
		e.printStackTrace();
		return null;
	}
	return wb;
}
 
Example 6
Source File: TestDeclarativeSpreadsheetWebScriptGet.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
protected void populateBody(Object resource, Workbook workbook, Sheet sheet, List<QName> properties) throws IOException
{
    // Set the sheet name
    workbook.setSheetName(0, "test");
}
 
Example 7
Source File: SampleTest.java    From xlsmapper with Apache License 2.0 4 votes vote down vote up
/**
 * {@link XlsSheet} - 正規表現の場合のシートのコピー
 */
@Test
public void test_Sheet_regex_clone() throws Exception {

    // 正規表現による複数のシートを出力する場合。
    // 書き込み時に、シート名を設定して、一意に関連づけます。
    SheetRegexClone sheet1 = new SheetRegexClone();
    sheet1.sheetName = "Sheet_1"; // シート名の設定

    SheetRegexClone sheet2 = new SheetRegexClone();
    sheet2.sheetName = "Sheet_2"; // シート名の設定

    SheetRegexClone sheet3 = new SheetRegexClone();
    sheet3.sheetName = "Sheet_3"; // シート名の設定

    SheetRegexClone[] sheets = new SheetRegexClone[]{sheet1, sheet2, sheet3};

    // シートのクローン
    Workbook workbook = WorkbookFactory.create(new FileInputStream("src/test/data/sample_template.xlsx"));
    Sheet templateSheet = workbook.getSheet("XlsSheet(regexp)");
    for(SheetRegexClone sheetObj : sheets) {
        int sheetIndex = workbook.getSheetIndex(templateSheet);
        Sheet cloneSheet = workbook.cloneSheet(sheetIndex);
        workbook.setSheetName(workbook.getSheetIndex(cloneSheet), sheetObj.sheetName);
    }

    // コピー元のシートを削除する
    workbook.removeSheetAt(workbook.getSheetIndex(templateSheet));

    // クローンしたシートファイルを、一時ファイルに一旦出力する。
    File cloneTemplateFile = File.createTempFile("template", ".xlsx");
    workbook.write(new FileOutputStream(cloneTemplateFile));

    // 複数のシートの書き込み
    XlsMapper xlsMapper = new XlsMapper();
    xlsMapper.saveMultiple(
            new FileInputStream(cloneTemplateFile), // クローンしたシートを持つファイルを指定する
            new FileOutputStream(new File(OUT_DIR, "sample_out.xlsx")),
            sheets);

}