poi读写Excel文件

来源:互联网 发布:js面向对象是什么 编辑:程序博客网 时间:2024/05/21 14:07
 
 
package dome.exp;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.text.DecimalFormat;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;public class ReadExcel {static String workStr = System.getProperty("user.dir");// 获取工程路径public static void main(String[] args) throws IOException {readExp();}/** * 读取xls文件 *  * @throws IOException */public static void readExp() throws IOException {InputStream is = new FileInputStream(workStr + "\\workbook2.xls");HSSFWorkbook wb = new HSSFWorkbook(is);// 获取工作簿// HSSFSheet sheet = wb.getSheetAt(0);//获取表int sheetNum = wb.getNumberOfSheets();// 获取表的个数;for (int i = 0; i < sheetNum; i++) {HSSFSheet sheet = wb.getSheetAt(i);// 获取当前的表System.out.println("当前表为:" + sheet.getSheetName());int rowNum = sheet.getLastRowNum();// 获取行数;for (int j = 0; j < rowNum; j++) {HSSFRow currRow = sheet.getRow(j);// 获取当前行int cellNum = currRow.getLastCellNum();// 获取列数for (int k = 0; k < cellNum; k++) {HSSFCell currCell = currRow.getCell(k);// 获取当前行列的单元格// 判断单元格的数据类型switch (currCell.getCellType()) {case HSSFCell.CELL_TYPE_NUMERIC:// 数值型// 如果是日期类型的// System.out.println("-----"+currCell.getCellStyle().getDataFormat());if (HSSFDateUtil.isCellDateFormatted(currCell)) {// 对于带中文年月日的无法判断SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd" + "\t");// System.out.print(sdf.format(currCell.getDateCellValue())+"\t");String date = sdf.format(HSSFDateUtil.getJavaDate(currCell.getNumericCellValue()));System.out.print(date);// System.out.print(currCell.getStringCellValue()+"日期"+"\t");} else {// 纯数字型//对数字的处理if (currCell.getNumericCellValue() > Double.MAX_VALUE) {DecimalFormat df = new DecimalFormat("#");// 解决电话号码用指数形式显示的问题// System.out.print(df.format(currCell.getNumericCellValue())+"\t");} else {System.out.print(currCell.getNumericCellValue()+ "\t");}// System.out.print(currCell.getStringCellValue()+"\t");}break;case HSSFCell.CELL_TYPE_STRING:// 字符串类型System.out.print(currCell.getStringCellValue() + "\t");break;default:System.out.print("");break;}}System.out.println();// 换行}}}}
 
 
 
 
<img src="http://img.blog.csdn.net/20141011233535615?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMDIzMjExMg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /><img src="http://img.blog.csdn.net/20141011233627033?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMDIzMjExMg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /><img src="http://img.blog.csdn.net/20141011233611261?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMDIzMjExMg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
<pre class="java" name="code">package dome.exp;import java.io.FileOutputStream;import java.io.IOException;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.hssf.util.CellRangeAddress;import org.apache.poi.hssf.util.HSSFColor;/** * 创建excel文件 *  * @author qozi *  */public class DoExcel {static String workStr = System.getProperty("user.dir");public static void main(String[] args) throws IOException {createBlack();// 创建一个空的excel文件//createDetail();//创建一个有内容的excel文件}/** * 创建空excel文件 *  * @throws IOException */public static void createBlack() throws IOException {System.out.println(workStr);// 获取当前项目路径// 创建一个excel文件HSSFWorkbook wb = new HSSFWorkbook();FileOutputStream fileOut = new FileOutputStream(workStr+ "\\workbook.xls");wb.write(fileOut);fileOut.close();}/** * 创建并操作excel文件的内容 * @throws IOException  */@SuppressWarnings("deprecation")public static void createDetail() throws IOException {HSSFWorkbook wb = new HSSFWorkbook();// 创建HSSFWorkbook对象HSSFSheet sheet = wb.createSheet("new sheet");// 创建HSSFSheet对象HSSFRow row = sheet.createRow(0);// 在sheet里创建一行,参数为行号(第一行,此处可想象成数组)HSSFCell cell = row.createCell(0);//在row里新建cell,参数为列号(第一列)cell.setCellValue(1);row.createCell(1).setCellValue(1.2);row.createCell(2).setCellValue("test");row.createCell(3).setCellValue(true);HSSFCellStyle cellStyle = wb.createCellStyle();//新建cell样式cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));cellStyle.setAlignment(HSSFCellStyle.VERTICAL_CENTER);HSSFCell dCell = row.createCell(4);dCell.setCellValue(new Date());dCell.setCellStyle(cellStyle);HSSFCell csCell= row.createCell(5);//csCell.setEncoding(HSSFCell.ENCODING_UTF_16);csCell.setCellValue("中文测试——chinese words test");row.createCell(6).setCellType(HSSFCell.CELL_TYPE_ERROR);//sheet.addMergedRegion(new Region(1,2,0,6));CellRangeAddress cra = new CellRangeAddress(1, 2, 0, 6);sheet.addMergedRegion(cra);//合并单元格HSSFCellStyle style = wb.createCellStyle();style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//设置对齐方式,还有很多属性都可以通过这个属性设置。如字体等//style.setFillBackgroundColor(HSSFColor.BLUE.index);//设置背景颜色style.setFillForegroundColor(HSSFColor.GREEN.index);HSSFCell cell2 = sheet.createRow(1).createCell(0);cell2.setCellValue("设置单元个样式");cell2.setCellStyle(style);FileOutputStream fileOut = new FileOutputStream(workStr+ "\\workbook1.xls");wb.write(fileOut);fileOut.close();System.out.println("创建完毕");}}

 


 


0 0
原创粉丝点击