1. jxl库
jxl.jar库下载地址:http://download.csdn.net/detail/yahohi/3826761
使用方法:
import java.io.FileInputStream;
import java.io.InputStream;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
public class xls {
public boolean readxlsByjxl(String filepath)
{
Workbook rwb;
try{
InputStream is = new FileInputStream(filepath);
rwb = Workbook.getWorkbook(is);
rwb.getNumberOfSheets();
Sheet st = rwb.getSheet("Sheet1");
int rows = st.getRows();
int cols = st.getColumns();
System.out.println("table name:"+st.getName());
System.out.println("total rows: "+rows);
System.out.println("total cols: "+cols);
for(int i = 0;i < rows;i ++)
{
Cell c1 = st.getCell(0, i);
Cell c2 = st.getCell(1, i);
Cell c3 = st.getCell(2, i);
Cell c4 = st.getCell(3, i);
System.out.println(c1.getContents()+" "+c2.getContents()+" "+c3.getContents()+" "+c4.getContents());
}
}
catch(Exception e)
{
e.printStackTrace();
}
return true;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
xls x = new xls();
x.readxlsByjxl("C:/test.xls");
}
}
2. poi库
poi.jar下载地址:http://download.csdn.net/detail/yahohi/3826817
poi.jar的详细使用文档:http://download.csdn.net/detail/yahohi/3826782
下面介绍和代码引用自http://www.cnblogs.com/dcba1112/archive/2011/06/06/2073952.html
Poi 读取文件的例子网上比较多, 但是很多都有小问题,今天我做了一个上传文件并显示XLS内容的模块,
参考了http://www.iteye.com/topic/388005 例程 ,
由于我用的3.7 的版本, 例程里的代码import 用的WorkbookFactory在3.7里已经不存在了,不知道
参考用的那个版本.所有我就使用了hssf和 ss相结合的调用.没有再使用例程方生产方式
例程 Workbook wb = WorkbookFactory.create(is);
我的代码: HSSFWorkbook wb = new HSSFWorkbook(fs);
例程有个漏洞就是为判断cell为空的情况,读取的数据列会自动向前
移动一列.这个问题很验证,我测试了一个下午才发现的.
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
- public class SummaryHSSF {
- public static void main(String[] args) throws IOException {
-
-
- Workbook wb = new HSSFWorkbook();
-
- CreationHelper helper = wb.getCreationHelper();
-
- Sheet sheet1 = wb.createSheet("HSSF_Sheet_1");
- Sheet sheet2 = wb.createSheet("HSSF_Sheet_2");
-
- Row row = null;
- Cell cell = null;
- for(int i=0;i<60;i=i+2){
-
- row = sheet1.createRow(i);
-
- row.setHeightInPoints(20);
-
- for(int j=0;j<25;j++){
-
- sheet1.autoSizeColumn(j+1, true);
-
- CellStyle cellStyle = SummaryHSSF.createStyleCell(wb);
-
- cell = row.createCell(j);
- if(j==0){
-
- cellStyle = SummaryHSSF.setCellStyleAlignment(cellStyle, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER);
-
- cellStyle.setFont(createFonts(wb));
-
- cell.setCellStyle(cellStyle);
-
- cell.setCellValue(true);
- }else if(j==1){
-
- cellStyle = SummaryHSSF.setCellStyleAlignment(cellStyle, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER);
-
- cellStyle = SummaryHSSF.setCellFormat(helper,cellStyle, "#,##0.0000");
-
- cellStyle.setFont(createFonts(wb));
-
- cell.setCellStyle(cellStyle);
-
- cell.setCellValue(new Double(2008.2008));
- }else if(j==2){
- cellStyle = SummaryHSSF.setCellStyleAlignment(cellStyle, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER);
- cellStyle.setFont(createFonts(wb));
- cell.setCellStyle(cellStyle);
- cell.setCellValue(helper.createRichTextString("RichString"+i+j));
- }else if(j==3){
- cellStyle = SummaryHSSF.setCellStyleAlignment(cellStyle, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER);
- cellStyle = SummaryHSSF.setCellFormat(helper,cellStyle, "MM-yyyy-dd");
- cell.setCellStyle(cellStyle);
- cell.setCellValue(new Date());
- }else if(j==24){
- cellStyle = SummaryHSSF.setCellStyleAlignment(cellStyle, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER);
- cellStyle.setFont(createFonts(wb));
-
- cell.setCellFormula("SUM(E"+(i+1)+":X"+(i+1)+")");
- }else{
- cellStyle = SummaryHSSF.setCellStyleAlignment(cellStyle, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER);
- cellStyle = SummaryHSSF.setFillBackgroundColors(cellStyle,IndexedColors.ORANGE.getIndex(),IndexedColors.ORANGE.getIndex(),CellStyle.SOLID_FOREGROUND);
- cell.setCellStyle(cellStyle);
- cell.setCellValue(1);
- }
- }
- }
-
- OutputStream os = new FileOutputStream(new File("c://SummaryHSSF.xls"));
- wb.write(os);
- os.close();
- }
-
-
-
-
-
- public static CellStyle createStyleCell(Workbook wb){
- CellStyle cellStyle = wb.createCellStyle();
-
- cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
- cellStyle.setBorderTop(CellStyle.BORDER_THIN);
- cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
- cellStyle.setBorderRight(CellStyle.BORDER_THIN);
-
- cellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
- cellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
- cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
- cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
- return cellStyle;
- }
-
-
-
-
-
-
-
-
-
- public static CellStyle setCellStyleAlignment(CellStyle cellStyle,short halign,short valign){
-
- cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
-
- cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
- return cellStyle;
- }
-
-
-
-
-
-
-
- public static CellStyle setCellFormat(CreationHelper helper,CellStyle cellStyle,String fmt){
-
- cellStyle.setDataFormat(helper.createDataFormat().getFormat(fmt));
- return cellStyle;
- }
-
-
-
-
-
-
-
-
- public static CellStyle setFillBackgroundColors(CellStyle cellStyle,short bg,short fg,short fp){
-
- cellStyle.setFillForegroundColor(fg);
- cellStyle.setFillPattern(fp);
- return cellStyle;
- }
-
-
-
-
-
- public static Font createFonts(Workbook wb){
-
- Font font = wb.createFont();
-
- font.setFontName("黑体");
-
- font.setColor(HSSFColor.BLUE.index);
-
- font.setItalic(true);
-
- font.setFontHeight((short)300);
- return font;
- }
- }
读取Excel文件
- public class ReadExcel {
- public static void main(String[] args) throws Exception {
- InputStream is = new FileInputStream(new File("c://SummaryHSSF.xls"));
-
- Workbook wb = WorkbookFactory.create(is);
- //HSSFWorkbook wb = new HSSFWorkbook(is);
-
- Sheet sheet = wb.getSheetAt(0);
-
- for(Row row : sheet){
- for(Cell cell : row){
- //实际读取中这里需要判断为空的情况
- //if(cell == null){
- //空处理
- //}
-
-
- switch(cell.getCellType()){
- case Cell.CELL_TYPE_BOOLEAN:
-
- System.out.print(cell.getBooleanCellValue()+" ");
- break;
- case Cell.CELL_TYPE_NUMERIC:
-
- if(DateUtil.isCellDateFormatted(cell)){
-
- System.out.print(cell.getDateCellValue()+" ");
- }else{
-
- System.out.print(cell.getNumericCellValue()+" ");
- }
- break;
- case Cell.CELL_TYPE_FORMULA:
-
- System.out.print(cell.getCellFormula()+" ");
- break;
- case Cell.CELL_TYPE_STRING:
-
- System.out.print(cell.getRichStringCellValue().toString()+" ");
- break;
- }
- }
- System.out.println("");
- }
- }
- }
还有一种传统的读法
- Sheet sheet = wb.getSheetAt(0);
- for (Iterator rit = sheet.rowIterator(); rit.hasNext(); ) {
- Row row = (Row)rit.next();
- for (Iterator cit = row.cellIterator(); cit.hasNext(); ) {
- Cell cell = (Cell)cit.next();
-
- }
- }
- HSSFSheet sheet = wb.getSheetAt(0);
- for (Iterator<HSSFRow> rit = (Iterator<HSSFRow>)sheet.rowIterator(); rit.hasNext(); ) {
- HSSFRow row = rit.next();
- for (Iterator<HSSFCell> cit = (Iterator<HSSFCell>)row.cellIterator(); cit.hasNext(); ) {
- HSSFCell cell = cit.next();
-
- }
- }
仅供参考,谢谢。