- package com.poi.second;
- import java.io.FileOutputStream;
- import java.util.Date;
- import org.apache.poi.hssf.usermodel.HSSFCell;
- import org.apache.poi.hssf.usermodel.HSSFCellStyle;
- import org.apache.poi.hssf.usermodel.HSSFDataFormat;
- import org.apache.poi.hssf.usermodel.HSSFRow;
- import org.apache.poi.hssf.usermodel.HSSFSheet;
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- import org.apache.poi.ss.usermodel.CreationHelper;
- /**
- * 写excel
- * @author 钟林森
- *
- */
- public class ExcelWrite {
- public static void main(String[] args) throws Exception{
- //创建一个工作簿 即excel文件,再在该文件中创建一个sheet
- HSSFWorkbook wb=new HSSFWorkbook();
- HSSFSheet sheet=wb.createSheet("第一个sheet");
- //在sheet中创建一行
- HSSFRow row=sheet.createRow(0);
- //在该行写入各种类型的数据
- row.createCell(0).setCellValue(true);
- row.createCell(1).setCellValue("钟林森");
- row.createCell(2).setCellValue(23);
- //设置保留两位小数
- HSSFCell cell=row.createCell(3);
- cell.setCellValue(6000);
- HSSFCellStyle cellStyle = wb.createCellStyle();
- cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));
- cell.setCellStyle(cellStyle);
- //在写入 日期格式的 数据需要进行特殊处理(这是一种 简单的处理方式)
- CreationHelper createHelper=wb.getCreationHelper();
- HSSFCellStyle style=wb.createCellStyle();
- style.setDataFormat(createHelper.createDataFormat().getFormat("yyyy-MM-dd"));
- cell=row.createCell(4);
- cell.setCellValue(new Date());
- cell.setCellStyle(style);
- //最后写回磁盘
- FileOutputStream out=new FileOutputStream("E:\\java_BasicWeb\\someFiles\\excel写数据.xls");
- wb.write(out);
- out.close();
- System.out.println("写完了!");
- }
- }
- package com.poi.second;
- import java.io.FileInputStream;
- import java.io.InputStream;
- import java.text.SimpleDateFormat;
- import org.apache.poi.hssf.usermodel.HSSFCell;
- import org.apache.poi.hssf.usermodel.HSSFDateUtil;
- import org.apache.poi.hssf.usermodel.HSSFRow;
- import org.apache.poi.hssf.usermodel.HSSFSheet;
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- import org.apache.poi.poifs.filesystem.POIFSFileSystem;
- /**
- * 读excel
- * @author 钟林森
- *
- */
- public class ExcelRead {
- public static void main(String[] args) throws Exception {
- //读取一个excel表的内容
- InputStream stream = new FileInputStream("E:\\java_BasicWeb\\someFiles\\excel读数据.xls");
- POIFSFileSystem fs = new POIFSFileSystem(stream);
- HSSFWorkbook wb = new HSSFWorkbook(fs);
- //获取excel表的第一个sheet
- HSSFSheet sheet = wb.getSheetAt(0);
- if (sheet == null) {
- return;
- }
- //遍历该sheet的行
- for (int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++) {
- HSSFRow row = sheet.getRow(rowNum);
- if (row == null) {
- continue;
- }
- //再遍历改行的所有列
- for(int cellNum = 0; cellNum <= row.getLastCellNum(); cellNum++) {
- HSSFCell cell = row.getCell(cellNum);
- if (cell == null) {
- continue;
- }
- String strVal=readCellSecondMethod(cell);
- if (cellNum==2) {
- strVal=strVal.contains(".")?strVal.substring(0, strVal.indexOf(".")):strVal;
- }
- System.out.print(" " + strVal);
- //System.out.print(" " + readCellFirstMethod(cell));
- //System.out.print(" " + readCellSecondMethod(cell));
- }
- System.out.println();
- }
- stream.close();
- }
- /**第一种方法
- * 读取excel单元格的内容并针对其type进行不同的处理,
- * 其中就包含 读取excel表格中日期格式的cell
- * @param cell
- * @return
- */
- public static String readCellFirstMethod(HSSFCell cell) {
- if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
- return String.valueOf(cell.getBooleanCellValue());
- } else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
- if (HSSFDateUtil.isCellDateFormatted(cell)) {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- return sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue())).toString();
- }
- return String.valueOf(cell.getNumericCellValue());
- } else {
- return cell.getStringCellValue();
- }
- }
- /**第二种方法
- * 读取excel单元格的内容并针对其type进行不同的处理,
- * 其中就包含 读取excel表格中日期格式的cell
- * @param cell
- * @return
- */
- public static String readCellSecondMethod(HSSFCell cell) {
- //DecimalFormat df = new DecimalFormat("#");
- if (cell == null) {
- return "";
- }
- switch (cell.getCellType()) {
- //数字
- case HSSFCell.CELL_TYPE_NUMERIC:
- //日期格式的处理
- if (HSSFDateUtil.isCellDateFormatted(cell)) {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- return sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue())).toString();
- }
- return String.valueOf(cell.getNumericCellValue());
- //return df.format(cell.getNumericCellValue());
- //字符串
- case HSSFCell.CELL_TYPE_STRING:
- return cell.getStringCellValue();
- //公式
- case HSSFCell.CELL_TYPE_FORMULA:
- return cell.getCellFormula();
- //空白
- case HSSFCell.CELL_TYPE_BLANK:
- return "";
- //布尔取值
- case HSSFCell.CELL_TYPE_BOOLEAN:
- return cell.getBooleanCellValue() + "";
- //错误类型
- case HSSFCell.CELL_TYPE_ERROR:
- return cell.getErrorCellValue() + "";
- }
- return "";
- }
- }