LoginSignup
0
0

SpringBootでApachePOIを使ったxlsxにセル書式設定

Last updated at Posted at 2024-04-13

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

概要

xlsxの生成とダウンロードは前回の記事でやってるので説明は省略。
枠線、背景色、フォント、文字揃え位置などの書式設定についてがメインです。

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.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
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_ = sheet_.createRow(1);
		Cell cell_1_ = row_.createCell(CellReference.convertColStringToIndex("B"));
		cell_1_.setCellValue("私とお茶しない?");
		
		
		//---------------------------------------------------------------------------------------------
		//セル書式設定。今回のメインはここ!
		
		CellStyle cell_style_ = workbook_.createCellStyle();
		
		//枠線。
		cell_style_.setBorderTop(BorderStyle.THIN);
		cell_style_.setBorderRight(BorderStyle.THIN);
		cell_style_.setBorderBottom(BorderStyle.THIN);
		//左の枠線だけ他と変えてみます。
		cell_style_.setBorderLeft(BorderStyle.MEDIUM_DASH_DOT);
		cell_style_.setLeftBorderColor(IndexedColors.BLUE1.getIndex());
		
		//テキスト回り込み。
		cell_style_.setWrapText(true);
		
		//縦位置揃え。
		cell_style_.setVerticalAlignment(VerticalAlignment.TOP);
		
		//横位置揃え。
		cell_style_.setAlignment(HorizontalAlignment.CENTER);
		
		//背景色。
		cell_style_.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
		cell_style_.setFillPattern(FillPatternType.BIG_SPOTS);
		
		//フォント。
		Font font_ = workbook_.createFont();
		font_.setFontName("MS 明朝");
		font_.setFontHeightInPoints((short) 15);
		cell_style_.setFont(font_);
		
		//セル生成。
		Cell cell_2_ = row_.createCell(CellReference.convertColStringToIndex("D"));
		cell_2_.setCellValue("お日様が出たら日光浴、お月様が出たら月光浴〜");
		
		//作ったセルスタイル(書式設定)をセルに適用。
		cell_2_.setCellStyle(cell_style_);
		
		
		//---------------------------------------------------------------------------------------------
		//ダウンロード。
		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

この記事の続き

バージョン

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