java poi 写入Excel后读取公式值问题
不用改公式,只需要用cell.getNumericCellValue()获取,读出来就是正确的值了,如果你读出来的还有问题,说明你其他的地方写的还有问题。
Java利用POI导入excel表格并将数据存到数据库的问题
当有合并表情况下,认为左上角单元格据,意思就是 标题 认为是a1,但是当你循环遍历这样的合并表格的话,数据是会重复的,即把合并的单元格拆分后每个单元格的数据都是一样的,这样就需要你判断过滤了。至于你这种单元格拼接的话就没有什么办法了,读取出单元格数据循环判断,然后存入数据库就行了
poi如何去写入excel文件
package mon.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 = pile("^0|[1-9]\\d*(\\.\\d )?$");
return pattern.matcher(str).matches();
}
}