上一篇讲解了一下如何读取excel,那在这篇就讲一下如何去写文件吧! 同样是用poi类工具去实现的。
- package com.common.util;
- import java.util.List;
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- import org.apache.poi.ss.usermodel.Cell;
- import org.apache.poi.ss.usermodel.CellStyle;
- import org.apache.poi.ss.usermodel.Font;
- 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.xssf.usermodel.XSSFCellStyle;
- public class ExcelUtil{
- public static HSSFWorkbook exportExcelForStudent(List studentList
- ) { //创建excel文件对象
- HSSFWorkbook wb = new HSSFWorkbook();
- //创建一个张表
- Sheet sheet = wb.createSheet();
- //创建第一行
- Row row = sheet.createRow(0);
- //创建第二行
- Row row1 = sheet.createRow(1);
- // 文件头字体
- Font font0 = createFonts(wb, Font.BOLDWEIGHT_BOLD, "宋体", false,
- (short) 200);
- Font font1 = createFonts(wb, Font.BOLDWEIGHT_NORMAL, "宋体", false,
- (short) 200);
- // 合并第一行的单元格
- sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 1));
- //设置第一列的文字
- createCell(wb, row, 0, “总数”, font0);
- //合并第一行的2列以后到8列(不包含第二列)
- sheet.addMergedRegion(new CellRangeAddress(0, 0, 2, 8));
- //设置第二列的文字
- createCell(wb, row, 2, “基本信息”, font0);
- //给第二行添加文本
- createCell(wb, row1, 0, "序号", font1);
- createCell(wb, row1, 1, "版本", font1);
- createCell(wb, row1, 2, "姓名", font1);
- createCell(wb, row1, 3, "性别", font1);
- createCell(wb, row1, 4, "年龄", font1);
- createCell(wb, row1, 5, "年级", font1);
- createCell(wb, row1, 6, "学校", font1);
- createCell(wb, row1, 7, "父母名称", font1);
- createCell(wb, row1, 8, "籍贯", font1);
- createCell(wb, row1, 9, "联系方式", font1);
- //第三行表示
- int l = 2;
- //这里将学员的信心存入到表格中
- for (int i = 0; i < studentList.size(); i++) {
- //创建一行
- Row rowData = sheet.createRow(l++);
- Student stu = studentList.get(i);
- createCell(wb, rowData, 0, String.valueOf(i + 1), font1);
- createCell(wb, rowData, 1, "3.0", font1);
- createCell(wb, rowData, 2, stu.getName(), font1);
- createCell(wb, rowData, 3, stu.getStudentsex(), font1);
- createCell(wb, rowData, 4, stu.getStudentage(), font1);
- createCell(wb, rowData, 5, stu.getGrade().getName(), font1);
- createCell(wb, rowData, 6, stu.getStudentschool(), font1);
- createCell(wb, rowData, 7, stu.getparents(), font1);
- createCell(wb, rowData, 8, stu.getStudentprovince()+stu.getStudentcity()+stu.getStudentarea(), font1);
- createCell(wb, rowData, 9, stu.getContact(), font1);
- }
- return wb;
- }
- /**
- * 创建单元格并设置样式,值
- *
- * @param wb
- * @param row
- * @param column
- * @param
- * @param
- * @param value
- */
- public static void createCell(Workbook wb, Row row, int column,
- String value, Font font) {
- Cell cell = row.createCell(column);
- cell.setCellValue(value);
- CellStyle cellStyle = wb.createCellStyle();
- cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
- cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_BOTTOM);
- cellStyle.setFont(font);
- cell.setCellStyle(cellStyle);
- }
- /**
- * 设置字体
- *
- * @param wb
- * @return
- */
- public static Font createFonts(Workbook wb, short bold, String fontName,
- boolean isItalic, short hight) {
- Font font = wb.createFont();
- font.setFontName(fontName);
- font.setBoldweight(bold);
- font.setItalic(isItalic);
- font.setFontHeight(hight);
- return font;
- }
- /**
- * 判断是否为数字
- *
- * @param str
- * @return
- */
- public static boolean isNumeric(String str) {
- if (str == null || "".equals(str.trim()) || str.length() > 10)
- return false;
- Pattern pattern = Pattern.compile("^0|[1-9]\\d*(\\.\\d+)?$");
- return pattern.matcher(str).matches();
- }
- }
相关推荐
Poi 写入EXCEL 插入一行Poi 写入EXCEL 插入一行
利用POI读取excel写入到word 利用POI读取excel写入到word 利用POI读取excel写入到word,压缩包里包含了jar包
本代码案例使用java语言,maven管理jar包,POI技术将数据写入到EXCEL文档内。
使用POI操作Excel文件(写)可参考:http://blog.csdn.net/flyever581549/archive/2008/11/11/3275850.aspx
POI中设置Excel单元格格式
使用poi解析excel文件,并将数据写入到数据库 项目说明 这个项目实现的功能是读取excel文件中的数据,解析并写入数据库。 读取的excel文件位于项目目录下的 excel\0805.xlsx 使用IntelliJ IDEA开发此项目 使用MYSQL...
poi读写excel,poi总结; poi读写excel,poi总结 poi读写excel,poi总结
POI向excel中插入图片
POI处理excel文件,主要包括Excel的读取、写入,各种数据格式处理、单元格合并、注释、下拉列表及单元格的边框、背景色、宽度调整等。
poi写入excel ,从excel中导出数据
利用 Apache POI 对 excel 操作并存入MySQL数据库的一个小demo。
通过Jxl与poi读取和写入excel文件,并且区分2007和2003版的excel格式
注意:该资源属于写数据到Excel,而非读取Excel数据保存到数据库,保存到数据库的例子资源:http://download.csdn.net/download/a13432421434/9620001
采用poi3.8解析excel文件,内含实例代码和jar包
java+poi+excel读取写入等操作所有jar(最新支持excel2013)Java实用源码整理learns
poi读写excel表的实例,用java实现,在eclipse里直接运行
在界面上有一个TextField,一个浏览按钮,一个导入按钮。当选择浏览按钮时,弹出文本选择窗口,选择Excel文件之后,将路径显示在TextField中。点击导入,将Excel中的数据保存到SqlServer数据库中。
阐述如何用POI来读取/写入完整的Excel文件。
Java用POI实现根据Excel表格模板生成新的Excel并实现数据输出...
1、POI_EXCEL包下分别有两个类,一个是读取excel内容,一个是想excel写入内容 2、cn.itcast包下的所有包,是为了实现从excel写入到数据库中,和从数据库写入到excel中 注意事项: 1、需要导入maven工程 2、使用...