---
title: "poi写入Excel"
author: "vergilwang"
date: "2015-02-25T16:56:04Z"
tags: []
categories: ["编程语言"]
slug: "poi-excel"
draft: false
lastmod: "2015-02-25T16:56:04Z"
id: 2186676
url: "https://vergilwang.iteye.com/blog/2186676"
---

上一篇讲解了一下如何读取excel，那在这篇就讲一下如何去写文件吧！ 同样是用poi类工具去实现的。

 

Java代码  [![收藏代码](http://kuaile863.iteye.com/images/icon_star.png)]()

001. package com.common.util;
002. import java.util.List;
003. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
004. import org.apache.poi.ss.usermodel.Cell;
005. import org.apache.poi.ss.usermodel.CellStyle;
006. import org.apache.poi.ss.usermodel.Font;
007. import org.apache.poi.ss.usermodel.Row;
008. import org.apache.poi.ss.usermodel.Sheet;
009. import org.apache.poi.ss.usermodel.Workbook;
010. import org.apache.poi.ss.util.CellRangeAddress;
011. import org.apache.poi.xssf.usermodel.XSSFCellStyle;
012. public class ExcelUtil{
013. public static HSSFWorkbook exportExcelForStudent(List studentList
014. ) {     //创建excel文件对象
015. HSSFWorkbook wb = new HSSFWorkbook();
016. //创建一个张表
017. Sheet sheet = wb.createSheet();
018. //创建第一行
019. Row row = sheet.createRow(0);
020. //创建第二行
021. Row row1 = sheet.createRow(1);
022. // 文件头字体
023. Font font0 = createFonts(wb, Font.BOLDWEIGHT\_BOLD, "宋体", false,
024. (short) 200);
025. Font font1 = createFonts(wb, Font.BOLDWEIGHT\_NORMAL, "宋体", false,
026. (short) 200);
027. // 合并第一行的单元格
028. sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 1));
029. //设置第一列的文字
030. createCell(wb, row, 0, “总数”, font0);
031. //合并第一行的2列以后到8列（不包含第二列）
032. sheet.addMergedRegion(new CellRangeAddress(0, 0, 2, 8));
033. //设置第二列的文字
034. createCell(wb, row, 2, “基本信息”, font0);
035. //给第二行添加文本
036. createCell(wb, row1, 0, "序号", font1);
037. createCell(wb, row1, 1, "版本", font1);
038. createCell(wb, row1, 2, "姓名", font1);
039. createCell(wb, row1, 3, "性别", font1);
040. createCell(wb, row1, 4, "年龄", font1);
041. createCell(wb, row1, 5, "年级", font1);
042. createCell(wb, row1, 6, "学校", font1);
043. createCell(wb, row1, 7, "父母名称", font1);
044. createCell(wb, row1, 8, "籍贯", font1);
045. createCell(wb, row1, 9, "联系方式", font1);
046. //第三行表示
047. int l = 2;
048. //这里将学员的信心存入到表格中
049. for (int i = 0; i &lt; studentList.size(); i++) {
050. //创建一行
051. Row rowData = sheet.createRow(l++);
052. Student stu = studentList.get(i);
053. createCell(wb, rowData, 0, String.valueOf(i + 1), font1);
054. createCell(wb, rowData, 1, "3.0", font1);
055. createCell(wb, rowData, 2, stu.getName(), font1);
056. createCell(wb, rowData, 3, stu.getStudentsex(), font1);
057. createCell(wb, rowData, 4, stu.getStudentage(), font1);
058. createCell(wb, rowData, 5, stu.getGrade().getName(), font1);
059. createCell(wb, rowData, 6, stu.getStudentschool(), font1);
060. createCell(wb, rowData, 7, stu.getparents(), font1);
061. createCell(wb, rowData, 8, stu.getStudentprovince()+stu.getStudentcity()+stu.getStudentarea(), font1);
062. createCell(wb, rowData, 9, stu.getContact(), font1);
063. }
064. return wb;
065. }
066. /\**
067. * 创建单元格并设置样式,值
068. \*
069. * @param wb
070. * @param row
071. * @param column
072. * @param
073. * @param
074. * @param value
075. \*/
076. public static void createCell(Workbook wb, Row row, int column,
077. String value, Font font) {
078. Cell cell = row.createCell(column);
079. cell.setCellValue(value);
080. CellStyle cellStyle = wb.createCellStyle();
081. cellStyle.setAlignment(XSSFCellStyle.ALIGN\_CENTER);
082. cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL\_BOTTOM);
083. cellStyle.setFont(font);
084. cell.setCellStyle(cellStyle);
085. }
086. /\**
087. * 设置字体
088. \*
089. * @param wb
090. * @return
091. \*/
092. public static Font createFonts(Workbook wb, short bold, String fontName,
093. boolean isItalic, short hight) {
094. Font font = wb.createFont();
095. font.setFontName(fontName);
096. font.setBoldweight(bold);
097. font.setItalic(isItalic);
098. font.setFontHeight(hight);
099. return font;
100. }
101. /\**
102. * 判断是否为数字
103. \*
104. * @param str
105. * @return
106. \*/
107. public static boolean isNumeric(String str) {
108. if (str == null || "".equals(str.trim()) || str.length() &gt; 10)
109. return false;
110. Pattern pattern = Pattern.compile("^0|\[1-9]\\\\d\*(\\\\.\\\\d+)?$");
111. return pattern.matcher(str).matches();
112. }
113. }