LoginSignup
0
0

SpringBootでApachePOIを使ったxlsxに印刷設定

Last updated at Posted at 2024-04-13

この記事はこれの続きです

概要

xlsxの生成とダウンロードは以前の記事でやってるので説明は省略。
印刷設定がメインです。

『全ての列を1ページに印刷』は試行錯誤して、想定通りになるまでけっこう時間かかったので、そのノウハウも書いてます。

Controllerクラス

DemoController.java
package com.example.demo;


import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.PageMargin;
import org.apache.poi.ss.usermodel.PrintSetup;
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.ss.util.CellReference;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;


@Controller
public class DemoController {
	
	
	@GetMapping("/")
	public void getIndex(HttpServletResponse response) throws IOException {
		
		
		//---------------------------------------------------------------------------------------------
		//エクセルファイル生成。
		Workbook workbook_ = new XSSFWorkbook();
		Sheet sheet_ = workbook_.createSheet("ふーたお");
		
		Row row_;
		Cell cell_;
		
		row_ = sheet_.createRow(1);
		cell_ = row_.createCell(CellReference.convertColStringToIndex("B"));
		cell_.setCellValue("私とお茶しない?");
		cell_ = row_.createCell(CellReference.convertColStringToIndex("H"));
		cell_.setCellValue("私とお茶しない?");
		
		row_ = sheet_.createRow(40);
		cell_ = row_.createCell(CellReference.convertColStringToIndex("A"));
		cell_.setCellValue("私とお茶しない?");
		
		
		//---------------------------------------------------------------------------------------------
		//今回のメインはここ!
		
		PrintSetup print_setup_ = sheet_.getPrintSetup();
		
		//用紙サイズを指定しないと拡大縮小系設定をした際にレターサイズになってしまうので注意。
		print_setup_.setPaperSize(PrintSetup.A4_PAPERSIZE);
		
		//用紙横向き。
		print_setup_.setLandscape(true);
		
		//印刷タイトル行。
		sheet_.setRepeatingRows(
				new CellRangeAddress(
						0,//1行目から…
						3,//4行目までがタイトル行。
						-1,//列の左端指定は無し。
						-1//列の右端指定は無し。
				)
		);
		
		//印刷余白設定。
		sheet_.setMargin(PageMargin.HEADER, 0);
		sheet_.setMargin(PageMargin.FOOTER, 0);
		sheet_.setMargin(PageMargin.TOP, 0);
		sheet_.setMargin(PageMargin.RIGHT, 0);
		sheet_.setMargin(PageMargin.BOTTOM, 0);
		sheet_.setMargin(PageMargin.LEFT, 0);
		
		//全ての列を1ページに印刷。
		
		//setFitWidth()やsetFitHeight()より前に、
		//setFitToPage(true)をすることが『全ての列を1ページに印刷』には必須。
		sheet_.setFitToPage(true);
		
		//全列を1ページに収める。
		print_setup_.setFitWidth((short) 1);
		
		//setFitHeight()で明示的に0を設定することも『全ての列を1ページに印刷』には必須。
		print_setup_.setFitHeight((short) 0);
		
		
		//---------------------------------------------------------------------------------------------
		//ダウンロード。
		response.addHeader(
				"Content-Disposition", 
				"attachment; filename*=UTF-8''" + URLEncoder.encode("胡桃.xlsx", StandardCharsets.UTF_8.name()));
		
		ServletOutputStream stream_ = response.getOutputStream();
		workbook_.write(stream_);
		stream_.close();
		
		workbook_.close();
		
		
	}
	
	
}

実行結果

ブラウザーでhttp://localhost:8080にアクセスしてダウンロードできたxlsxを印刷プレビューすると…

image.png

image.png

image.png

この記事の続き

バージョン

Microsoft Windows [Version 10.0.22631.3447]
JAVA 17.0.8.1
Spring Boot v3.1.10
ApachePOI 5.2.5

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0