您的位置:首页 > 编程语言 > Java开发

poi 操作excel 的例子

2016-01-21 15:13 465 查看
package com;

import java.io.FileInputStream;
import java.util.Iterator;

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 Excel {

public static void main(String[] args) {
try {
// 创建一个文件输入流
FileInputStream is = new FileInputStream("D:\\test.xls");
// 得到工作薄
HSSFWorkbook workbook = new HSSFWorkbook(is);
// 得到工作表的张数
int num = workbook.getNumberOfSheets();
System.out.println("工作表的数目:" + num);
// 得到第一张工作表
HSSFSheet sheet = workbook.getSheetAt(0);
// 得到第一列的宽度
int width = sheet.getColumnWidth((short) 0);
System.out.println("第一列的宽度:" + width);
// 得到第一行的单元格迭代器
Iterator iter = sheet.getRow(0).cellIterator();

// 得到工作表的行数
int frnum = sheet.getFirstRowNum();
int lrnum = sheet.getLastRowNum();
int rnum = lrnum - frnum + 1;
System.out.println("工作表的行数为:" + rnum);
// 得到工作表的列数
int fcnum = sheet.getRow(0).getFirstCellNum();
int lcnum = sheet.getRow(0).getLastCellNum();
int cnum = lcnum - fcnum;
System.out.println("工作表的列数为:" + cnum);
// 打印第一行的每一个单元格的信息
while (iter.hasNext()) {
// 得到单元格
HSSFCell cell = (HSSFCell) iter.next();
// 单元格为字符串型的情况
if (cell.getCellType() == 1) {
System.out.println("第" + cell.getCellNum() + "个单元格:"
+ cell.getStringCellValue());
}
// 单元格为布尔型的情况
else if (HSSFCell.CELL_TYPE_BOOLEAN == cell.getCellType()) {
System.out.println("第" + cell.getCellNum() + "个单元格:"
+ cell.getBooleanCellValue());
}
// 单元格为数字型的情况
else if (HSSFCell.CELL_TYPE_NUMERIC == cell.getCellType()) {
System.out.println("第" + cell.getCellNum() + "个单元格:"
+ cell.getNumericCellValue());
}
// 单元格为日期型的情况
else if (HSSFDateUtil.isCellDateFormatted(cell)) {
System.out.println("第" + cell.getCellNum() + "个单元格:"
+ cell.getDateCellValue());
}
// 单元格为计算公式型的情况
else if (HSSFCell.CELL_TYPE_FORMULA == cell.getCellType()) {
System.out.println("第" + cell.getCellNum() + "个单元格:"
+ cell.getCellFormula());
}
// 单元格为错误型的情况
else if (HSSFCell.CELL_TYPE_ERROR == cell.getCellType()) {
System.out.println("第" + cell.getCellNum() + "个单元格:"
+ cell.getErrorCellValue());
}
// 单元格为空的情况
else if (HSSFCell.CELL_TYPE_BLANK == cell.getCellType()) {
System.out.println("第" + cell.getCellNum() + "个单元格:"
+ "单元格为空");
}
// 其他
else {
System.out.println("第" + cell.getCellNum() + "个单元格:"
+ "其他情形");
}
}
// 创建一个新行
HSSFRow row = sheet.createRow(rnum + 1);
// 创建一个新的单元格,并设置值
row.createCell((short) 0).setCellValue("测试");
// 打印新建单元格的值
System.out.println(row.getCell((short) 0).getStringCellValue());
} catch (Exception e) {
System.out.println(e.toString());
}

}

}
<pre class="java" name="code">package a;
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.HSSFColor;
public class tryy {
public static void main(String[] args) {
int i=0;
try{
HSSFWorkbook wb = new HSSFWorkbook();
// 建立新HSSFWorkbook对象
HSSFSheet sheet = wb.createSheet("new sheet");
// 建立新的sheet对象
// Create a row and put some cells in it.Rows are 0 based.
for(i=0;i<6;i++)
{
HSSFRow row = sheet.createRow((short) i);
// 建立新行
// Create a cell and put a value in it.
HSSFCell cell = row.createCell((short) 0);
// 建立新cell
cell.setCellValue(1);// 设置cell的整数类型的值
// Or do it on one line.
row.createCell((short) 1).setCellValue(1.2);
// 设置cell浮点类型的值
row.createCell((short) 2).setCellValue("test");
// 设置cell字符类型的值
row.createCell((short) 3).setCellValue(true);
// 设置cell布尔类型的值
HSSFCellStyle cellStyle = wb.createCellStyle();
// 建立新的cell样式
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
// 设置cell样式为定制的日期格式
HSSFCell dCell = row.createCell((short) 4);
dCell.setCellValue(new Date());
// 设置cell为日期类型的值
dCell.setCellStyle(cellStyle);
// 设置该cell日期的显示格式
HSSFCell csCell = row.createCell((short) 5);
// csCell.set
//csCell.setEncoding(HSSFCell.ENCODING_UTF_16);
// 设置cell编码解决中文高位字节截断
csCell.setCellValue("中文测试_Chinese Words Test");

// 设置背景色
HSSFCellStyle style = wb.createCellStyle();
style.setFillPattern(HSSFCellStyle.SPARSE_DOTS);
HSSFCell cell1 = row.createCell((short) 6);
cell1.setCellValue("X");
cell1.setCellStyle(style);

// 设置背景色
HSSFCellStyle style1 = wb.createCellStyle();
style1.setBorderBottom((short) 1);
/**
* 注意这句代码, style1.setFillPattern, 如果你在你的程序中不设置fill pattern,那么
* 你上面设置的前景色和背景色就显示不出来.网络上很多文章都没有设置fillpattern, 不知道那些达人 的机器是不是比我的机器智能很多.
*/
style1.setFillPattern(HSSFCellStyle.SPARSE_DOTS);
HSSFCell cell11 = row.createCell((short) 7);
cell11.setCellValue("X11");
cell11.setCellStyle(style1);

// 数字格式化
HSSFCellStyle st = wb.createCellStyle();
// 建立新的cell样式
st.setDataFormat(HSSFDataFormat.getBuiltinFormat("#,##0"));
HSSFCell cell12 = row.createCell((short) 8);
cell12.setCellValue((double) 10000000);
cell12.setCellStyle(st);

// 设置中西文结合字符串
row.createCell((short) 9).setCellType(HSSFCell.CELL_TYPE_ERROR);
// 建立错误cell
}
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("c:/workbook.xls");
wb.write(fileOut);
fileOut.close();
}catch(Exception e){}

}
4000

}
</pre>
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java