Skip to content

Instantly share code, notes, and snippets.

@asdf913
Last active March 20, 2025 12:29
Show Gist options
  • Save asdf913/e87ee27d9f5ab7b8ab283c0d15ae8a51 to your computer and use it in GitHub Desktop.
Save asdf913/e87ee27d9f5ab7b8ab283c0d15ae8a51 to your computer and use it in GitHub Desktop.
JPT Exam Mark Sheet (xlsx)
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import org.apache.commons.lang3.StringUtils;
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.CellType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
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.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.RegionUtil;
public class JptExamMarkSheetExcel {
public static void main(final String[] args) throws IOException {
//
final File file = new File("test.xlsx");
//
System.out.println(file.getAbsolutePath());
//
try (final Workbook wb = WorkbookFactory.create(true); final OutputStream os = new FileOutputStream(file)) {
//
final Sheet sheet = wb != null ? wb.createSheet() : null;
//
Row row = createRow(sheet, getPhysicalNumberOfRows(sheet, 0));
//
// Cell cell = createCell(row, getPhysicalNumberOfCells(row, 0));
//
Cell cell = null;
//
CellRangeAddress cra = null;
//
int rowNum = 0;
//
for (int i = 0; i < 25; i++) {
//
if ((cell = createCell(row, i)) == null) {
//
continue;
//
} // if
//
if (i == 8) {
//
setCellValue(cell, "ちょう");
//
addMergedRegion(sheet, new CellRangeAddress(rowNum = row.getRowNum(), rowNum, cell.getColumnIndex(),
cell.getColumnIndex() + 1));
//
} else if (i == 16) {
//
setCellValue(cell, "かい");
//
addMergedRegion(sheet, new CellRangeAddress(rowNum = row.getRowNum(), rowNum, cell.getColumnIndex(),
cell.getColumnIndex() + 1));
//
} // if
//
} // for
//
for (int i = 26; i < 51; i++) {
//
if ((cell = createCell(row, i)) == null) {
//
continue;
//
} // if
//
if (i == 34) {
//
setCellValue(cell, "どっ");
//
addMergedRegion(sheet, new CellRangeAddress(rowNum = row.getRowNum(), rowNum, cell.getColumnIndex(),
cell.getColumnIndex() + 1));
//
} else if (i == 42) {
//
setCellValue(cell, "かい");
//
addMergedRegion(sheet, new CellRangeAddress(rowNum = row.getRowNum(), rowNum, cell.getColumnIndex(),
cell.getColumnIndex() + 1));
//
} // if
//
} // for
//
// Splitter
//
int columnIndex = 0;
//
setCellValue(createCell(row, columnIndex = 25), " ");
//
addMergedRegion(sheet, cra = new CellRangeAddress(0, 24, columnIndex, columnIndex));
//
sheet.setColumnWidth(columnIndex, 400);
//
//
//
row = createRow(sheet, getPhysicalNumberOfRows(sheet, 0));
//
for (int i = 0; i < 25; i++) {
//
if ((cell = createCell(row, i)) == null) {
//
continue;
//
} // if
//
if (i == 8) {
//
setCellValue(cell, "聴");
//
addMergedRegion(sheet, new CellRangeAddress(rowNum = row.getRowNum(), rowNum, cell.getColumnIndex(),
cell.getColumnIndex() + 1));
//
} else if (i == 16) {
//
setCellValue(cell, "解");
//
addMergedRegion(sheet, new CellRangeAddress(rowNum = row.getRowNum(), rowNum, cell.getColumnIndex(),
cell.getColumnIndex() + 1));
//
} // if
//
} // for
//
for (int i = 26; i < 51; i++) {
//
if ((cell = createCell(row, i)) == null) {
//
continue;
//
} // if
//
if (i == 34) {
//
setCellValue(cell, "読");
//
addMergedRegion(sheet, new CellRangeAddress(rowNum = row.getRowNum(), rowNum, cell.getColumnIndex(),
cell.getColumnIndex() + 1));
//
} else if (i == 42) {
//
setCellValue(cell, "解");
//
addMergedRegion(sheet, new CellRangeAddress(rowNum = row.getRowNum(), rowNum, cell.getColumnIndex(),
cell.getColumnIndex() + 1));
//
} // if
//
} // for
//
// No.
//
row = createRow(sheet, getPhysicalNumberOfRows(sheet, 0));
//
for (int i = 0; i < 5; i++) {
//
setCellValue(cell = createCell(row, columnIndex = i * 5), "No.");
//
addMergedRegion(sheet,
cra = new CellRangeAddress(rowNum = row.getRowNum(), rowNum + 2, columnIndex, columnIndex));
//
sheet.setColumnWidth(columnIndex, 750);
//
setCellValue(cell = createCell(row, columnIndex = i * 5 + 1), "かいとうらん");
//
addMergedRegion(sheet,
cra = new CellRangeAddress(row.getRowNum(), row.getRowNum(), columnIndex, columnIndex + 3));
//
setCellValue(cell = createCell(row, columnIndex = i * 5 + 26), "No.");
//
sheet.setColumnWidth(columnIndex, 750);
//
addMergedRegion(sheet,
cra = new CellRangeAddress(row.getRowNum(), row.getRowNum() + 2, columnIndex, columnIndex));
//
setCellValue(cell = createCell(row, columnIndex = i * 5 + 27), "かいとうらん");
//
addMergedRegion(sheet,
cra = new CellRangeAddress(row.getRowNum(), row.getRowNum(), columnIndex, columnIndex + 3));
//
} // for
//
// 解答欄
//
row = createRow(sheet, getPhysicalNumberOfRows(sheet, 0));
//
for (int i = 0; i < 5; i++) {
//
setCellValue(cell = createCell(row, columnIndex = i * 5 + 1), "解答欄");
//
addMergedRegion(sheet, cra = new CellRangeAddress(rowNum = row.getRowNum(), row.getRowNum(),
columnIndex, columnIndex + 3));
//
setAlignment(getCellStyle(cell), HorizontalAlignment.CENTER);
//
setCellValue(cell = createCell(row, columnIndex = i * 5 + 27), "解答欄");
//
addMergedRegion(sheet, cra = new CellRangeAddress(rowNum = row.getRowNum(), row.getRowNum(),
columnIndex, columnIndex + 3));
//
setAlignment(getCellStyle(cell), HorizontalAlignment.CENTER);
//
} // for
//
// ABCD
//
row = createRow(sheet, getPhysicalNumberOfRows(sheet, 0));
//
for (int i = 0; i < 5; i++) {
//
for (int j = 0; j < 4; j++) {
//
setCellValue(cell = createCell(row, columnIndex = i * 5 + j + 1),
Character.valueOf((char) (97 + j)).toString());
//
sheet.setColumnWidth(columnIndex, 710);
//
setCellValue(cell = createCell(row, columnIndex = i * 5 + j + 27),
Character.valueOf((char) (97 + j)).toString());
//
sheet.setColumnWidth(columnIndex, 710);
//
} // for
//
} // for
//
for (int i = 0; i < 20; i++) {
//
row = createRow(sheet, getPhysicalNumberOfRows(sheet, 0));
//
for (int j = 0; j < 5; j++) {
//
setCellValue(cell = createCell(row, columnIndex = j * 5), Integer.toString(j * 20 + 1 + i));
//
setCellValue(cell = createCell(row, columnIndex = j * 5 + 1), "\u24D0");
//
setCellValue(cell = createCell(row, columnIndex = j * 5 + 2), "\u24D1");
//
setCellValue(cell = createCell(row, columnIndex = j * 5 + 3), "\u24D2");
//
setCellValue(cell = createCell(row, columnIndex = j * 5 + 4), "\u24D3");
//
setCellValue(cell = createCell(row, columnIndex = j * 5 + 26), Integer.toString(j * 20 + 101 + i));
//
setCellValue(cell = createCell(row, columnIndex = j * 5 + 27), "\u24D0");
//
setCellValue(cell = createCell(row, columnIndex = j * 5 + 28), "\u24D1");
//
setCellValue(cell = createCell(row, columnIndex = j * 5 + 29), "\u24D2");
//
setCellValue(cell = createCell(row, columnIndex = j * 5 + 30), "\u24D3");
//
} // for
//
} // for
//
CellStyle cellStyle = null;
//
Font font = null;
//
String stringCellValue = null;
//
for (int i = 0; sheet != null && i <= sheet.getLastRowNum(); i++) {
//
if ((row = sheet.getRow(i)) == null) {
//
continue;
//
} // if
//
for (int j = 0; j <= row.getLastCellNum(); j++) {
//
if ((cell = row.getCell(j)) == null) {
//
continue;
//
} // if
//
if ((cellStyle = wb.createCellStyle()) != null) {
//
cellStyle.setAlignment(HorizontalAlignment.CENTER);
//
cellStyle.setBorderBottom(BorderStyle.THIN);
//
cellStyle.setBorderLeft(BorderStyle.THIN);
//
cellStyle.setBorderRight(BorderStyle.THIN);
//
cellStyle.setBorderTop(BorderStyle.THIN);
//
if ((i == 0 || i == 1) && ((j >= 0 || j <= 8) || (j >= 11 || i <= 16) || (j >= 19 || i <= 34)
|| (j >= 37 || i <= 42) || (j >= 45))) {
//
if (i == 0) {
//
cellStyle.setBorderBottom(BorderStyle.NONE);
//
} else if (i == 1) {
//
cellStyle.setBorderTop(BorderStyle.NONE);
//
} // if
//
if (j != 24) {
//
cellStyle.setBorderRight(BorderStyle.NONE);
//
} // if
//
if (j == 0 || j == 26) {
//
cellStyle.setBorderLeft(BorderStyle.THIN);
//
} else if (j == 50) {
//
cellStyle.setBorderLeft(BorderStyle.NONE);
//
cellStyle.setBorderRight(BorderStyle.THIN);
//
} else if (j > 0) {
//
cellStyle.setBorderLeft(BorderStyle.NONE);
//
} // if
//
} // if
//
if ((font = wb.createFont()) != null) {
//
font.setFontHeightInPoints((short) 9);
//
if (Objects.equals(cell.getCellType(), CellType.STRING)) {
//
if (Objects.equals(stringCellValue = cell.getStringCellValue(), "かいとうらん")) {
//
font.setFontHeightInPoints((short) 7);
//
cellStyle.setBorderBottom(BorderStyle.NONE);
//
} else if (Objects.equals(stringCellValue, "解答欄")) {
//
font.setFontHeightInPoints((short) 11);
//
cellStyle.setBorderTop(BorderStyle.NONE);
//
} else if (contains(Arrays.asList("ちょう", "どっ"), stringCellValue)) {
//
font.setFontHeightInPoints((short) 7);
//
cellStyle.setBorderBottom(BorderStyle.NONE);
//
} else if (contains(Arrays.asList("a", "\u24D0"), stringCellValue)) {
//
cellStyle.setBorderRight(BorderStyle.NONE);
//
} else if (contains(Arrays.asList("b", "c", "\u24D1", "\u24D2"), stringCellValue)) {
//
cellStyle.setBorderLeft(BorderStyle.NONE);
//
cellStyle.setBorderRight(BorderStyle.NONE);
//
} else if (contains(Arrays.asList("d", "\u24D3"), stringCellValue)) {
//
cellStyle.setBorderLeft(BorderStyle.NONE);
//
} else if (Objects.equals("No.", stringCellValue)) {
//
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
//
} // if
//
} // if
//
} // if
//
cellStyle.setFont(font);
//
} // if
//
cell.setCellStyle(cellStyle);
//
} // for
//
} // for
//
final List<CellRangeAddress> cras = sheet != null ? sheet.getMergedRegions() : null;
//
for (int i = 0; cras != null && i < cras.size(); i++) {
//
if ((cra = cras.get(i)) == null || (row = sheet.getRow(cra.getFirstRow())) == null
|| (cell = row.getCell(cra.getFirstColumn())) == null) {
//
continue;
//
} // if
//
if (contains(Arrays.asList("かいとうらん", "解答欄"), cell.toString())) {
//
RegionUtil.setBorderRight(BorderStyle.THIN, cra, sheet);
//
} else if (StringUtils.isBlank(cell.toString())) {
//
RegionUtil.setBorderTop(BorderStyle.NONE, cra, sheet);
//
} // if
//
} // for
//
final PageMargin[] pageMargins = PageMargin.values();
//
for (int i = 0; sheet != null && pageMargins != null && i < pageMargins.length; i++) {
//
sheet.setMargin(pageMargins[i], 0.0125);
//
} // for
//
final PrintSetup printSetup = sheet != null ? sheet.getPrintSetup() : null;
//
if (printSetup != null) {
//
printSetup.setLandscape(true);
//
} // if
//
if (wb != null) {
//
wb.write(os);
//
} // if
//
} // try
//
}
private static boolean contains(final Collection<?> items, final Object item) {
return items != null && items.contains(item);
}
private static CellStyle getCellStyle(final Cell instance) {
return instance != null ? instance.getCellStyle() : null;
}
private static void setAlignment(final CellStyle instance, final HorizontalAlignment align) {
if (instance != null) {
instance.setAlignment(align);
}
}
private static int getPhysicalNumberOfRows(final Sheet instance, final int defaultValue) {
return instance != null ? instance.getPhysicalNumberOfRows() : defaultValue;
}
private static Row createRow(final Sheet instance, final int rownum) {
return instance != null ? instance.createRow(rownum) : null;
}
private static Cell createCell(final Row instance, final int column) {
return instance != null ? instance.createCell(column) : null;
}
private static int addMergedRegion(final Sheet instance, final CellRangeAddress region) {
return instance != null ? instance.addMergedRegion(region) : 0;
}
private static void setCellValue(final Cell instance, final String value) {
if (instance != null) {
instance.setCellValue(value);
}
}
}
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.4.0</version>
</dependency>
<!--https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.17.0</version>
</dependency>
</dependencies>
@asdf913
Copy link
Author

asdf913 commented Mar 20, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment