A. java poi怎麼獲取excel單元格的內容
js">packagee.sjtu.erplab.poi;
importjava.io.InputStream&ch=ww.xqy.chain"target="_blank"class="link-ke">FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.IOException;
importjava.io.InputStream;
importjava.text.SimpleDateFormat;
importjava.util.Date;
importjava.util.HashMap;
importjava.util.Map;
importorg.apache.poi.hssf.usermodel.HSSFCell;
importorg.apache.poi.hssf.usermodel.HSSFDateUtil;
importorg.apache.poi.hssf.usermodel.HSSFRow;
importorg.apache.poi.hssf.usermodel.HSSFSheet;
importorg.apache.poi.hssf.usermodel.HSSFWorkbook;
importorg.apache.poi.poifs.filesystem.POIFSFileSystem;
/**
*操作Excel表格的功能類
*/
publicclassExcelReader{
privatePOIFSFileSystemfs;
privateHSSFWorkbookwb;
privateHSSFSheetsheet;
privateHSSFRowrow;
/**
*讀取Excel表格表頭的內容
*@paramInputStream
*@returnString表頭內容的數組
*/
publicString[]readExcelTitle(InputStreamis){
try{
fs=newPOIFSFileSystem(is);
wb=newHSSFWorkbook(fs);
}catch(IOExceptione){
e.printStackTrace();
}
sheet=wb.getSheetAt(0);
row=sheet.getRow(0);
//標題總列數
intcolNum=row.getPhysicalNumberOfCells();
System.out.println("colNum:"+colNum);
String[]title=newString[colNum];
for(inti=0;i<colNum;i++){
//title[i]=getStringCellValue(row.getCell((short)i));
title[i]=getCellFormatValue(row.getCell((short)i));
}
returntitle;
}
/**
*讀取Excel數據內容
*@paramInputStream
*@returnMap包含單元格數據內容的Map對象
*/
publicMap<Integer,String>readExcelContent(InputStreamis){
Map<Integer,String>content=newHashMap<Integer,String>();
Stringstr="";
try{
fs=newPOIFSFileSystem(is);
wb=newHSSFWorkbook(fs);
}catch(IOExceptione){
e.printStackTrace();
}
sheet=wb.getSheetAt(0);
//得到總行數
introwNum=sheet.getLastRowNum();
row=sheet.getRow(0);
intcolNum=row.getPhysicalNumberOfCells();
//正文內容應該從第二行開始,第一行為表頭的標題
for(inti=1;i<=rowNum;i++){
row=sheet.getRow(i);
intj=0;
while(j<colNum){
//每個單元格的數據內容用"-"分割開,以後需要時用String類的replace()方法還原數據
//也可以將每個單元格的數據設置到一個javabean的屬性中,此時需要新建一個javabean
//str+=getStringCellValue(row.getCell((short)j)).trim()+
//"-";
str+=getCellFormatValue(row.getCell((short)j)).trim()+"";
j++;
}
content.put(i,str);
str="";
}
returncontent;
}
/**
*獲取單元格數據內容為字元串類型的數據
*
*@paramcellExcel單元格
*@returnString單元格數據內容
*/
(HSSFCellcell){
StringstrCell="";
switch(cell.getCellType()){
caseHSSFCell.CELL_TYPE_STRING:
strCell=cell.getStringCellValue();
break;
caseHSSFCell.CELL_TYPE_NUMERIC:
strCell=String.valueOf(cell.getNumericCellValue());
break;
caseHSSFCell.CELL_TYPE_BOOLEAN:
strCell=String.valueOf(cell.getBooleanCellValue());
break;
caseHSSFCell.CELL_TYPE_BLANK:
strCell="";
break;
default:
strCell="";
break;
}
if(strCell.equals("")||strCell==null){
return"";
}
if(cell==null){
return"";
}
returnstrCell;
}
/**
*獲取單元格數據內容為日期類型的數據
*
*@paramcell
*Excel單元格
*@returnString單元格數據內容
*/
privateStringgetDateCellValue(HSSFCellcell){
Stringresult="";
try{
intcellType=cell.getCellType();
if(cellType==HSSFCell.CELL_TYPE_NUMERIC){
Datedate=cell.getDateCellValue();
result=(date.getYear()+1900)+"-"+(date.getMonth()+1)
+"-"+date.getDate();
}elseif(cellType==HSSFCell.CELL_TYPE_STRING){
Stringdate=getStringCellValue(cell);
result=date.replaceAll("[年月]","-").replace("日","").trim();
}elseif(cellType==HSSFCell.CELL_TYPE_BLANK){
result="";
}
}catch(Exceptione){
System.out.println("日期格式不正確!");
e.printStackTrace();
}
returnresult;
}
/**
*根據HSSFCell類型設置數據
*@paramcell
*@return
*/
(HSSFCellcell){
Stringcellvalue="";
if(cell!=null){
//判斷當前Cell的Type
switch(cell.getCellType()){
//如果當前Cell的Type為NUMERIC
caseHSSFCell.CELL_TYPE_NUMERIC:
caseHSSFCell.CELL_TYPE_FORMULA:{
//判斷當前的cell是否為Date
if(HSSFDateUtil.isCellDateFormatted(cell)){
//如果是Date類型則,轉化為Data格式
//方法1:這樣子的data格式是帶時分秒的:2011-10-120:00:00
//cellvalue=cell.getDateCellValue().toLocaleString();
//方法2:這樣子的data格式是不帶帶時分秒的:2011-10-12
Datedate=cell.getDateCellValue();
SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-dd");
cellvalue=sdf.format(date);
}
//如果是純數字
else{
//取得當前Cell的數值
cellvalue=String.valueOf(cell.getNumericCellValue());
}
break;
}
//如果當前Cell的Type為STRIN
caseHSSFCell.CELL_TYPE_STRING:
//取得當前的Cell字元串
cellvalue=cell.getRichStringCellValue().getString();
break;
//默認的Cell值
default:
cellvalue="";
}
}else{
cellvalue="";
}
returncellvalue;
}
publicstaticvoidmain(String[]args){
try{
//對讀取Excel表格標題測試
InputStreamis=newFileInputStream("d:\test2.xls");
ExcelReaderexcelReader=newExcelReader();
String[]title=excelReader.readExcelTitle(is);
System.out.println("獲得Excel表格的標題:");
for(Strings:title){
System.out.print(s+"");
}
//對讀取Excel表格內容測試
InputStreamis2=newFileInputStream("d:\test2.xls");
Map<Integer,String>map=excelReader.readExcelContent(is2);
System.out.println("獲得Excel表格的內容:");
for(inti=1;i<=map.size();i++){
System.out.println(map.get(i));
}
}catch(FileNotFoundExceptione){
System.out.println("未找到指定路徑的文件!");
e.printStackTrace();
}
}
}
B. java操作poi怎麼更改excel中的數據
修改完需要寫入,也就是保存一下的。
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
 
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 
public class ChangeCell {
 
    @SuppressWarnings("deprecation")
    public static void main(String[] args) {
        String fileToBeRead = "C:\\exp.xls"; // excel位置
        int coloum = 1; // 比如你要獲取第1列
        try {
            HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(
                    fileToBeRead));
            HSSFSheet sheet = workbook.getSheet("Sheet1");
 
            for (int i = 0; i <= sheet.getLastRowNum(); i++) {
                HSSFRow row = sheet.getRow((short) i);
                if (null == row) {
                    continue;
                } else {
                    HSSFCell cell = row.getCell((short) coloum);
                    if (null == cell) {
                        continue;
                    } else {
                        System.out.println(cell.getNumericCellValue());
                        int temp = (int) cell.getNumericCellValue();
                        cell.setCellValue(temp + 1);
                    }
                }
            }
            FileOutputStream out = null;
            try {
                out = new FileOutputStream(fileToBeRead);
                workbook.write(out);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
 
    }
 
}
C. java poi怎麼導入excel數據
package poi;  
import java.io.FileInputStream;  
import java.io.IOException;  
import java.io.InputStream;  
import java.util.Iterator;  
import org.apache.poi.hssf.usermodel.HSSFCell;  
import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
import org.apache.poi.ss.usermodel.Cell;  
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.xssf.usermodel.XSSFWorkbook;  
   
public class ReadExcel001 {  
    public static void main(String[] args) {  
        readXml("D:/test.xlsx");  
        System.out.println("-------------");  
        readXml("d:/test2.xls");  
    }  
    public static void readXml(String fileName){  
        boolean isE2007 = false;    //判斷是否是excel2007格式  
        if(fileName.endsWith("xlsx"))  
            isE2007 = true;  
        try {  
            InputStream input = new FileInputStream(fileName);  //建立輸入流  
            Workbook wb  = null;  
            //根據文件格式(2003或者2007)來初始化  
            if(isE2007)  
                wb = new XSSFWorkbook(input);  
            else  
                wb = new HSSFWorkbook(input);  
            Sheet sheet = wb.getSheetAt(0);     //獲得第一個表單  
            Iterator<Row> rows = sheet.rowIterator(); //獲得第一個表單的迭代器  
            while (rows.hasNext()) {  
                Row row = rows.next();  //獲得行數據  
                System.out.println("Row #" + row.getRowNum());  //獲得行號從0開始  
                Iterator<Cell> cells = row.cellIterator();    //獲得第一行的迭代器  
                while (cells.hasNext()) {  
                    Cell cell = cells.next();  
                    System.out.println("Cell #" + cell.getColumnIndex());  
                    switch (cell.getCellType()) {   //根據cell中的類型來輸出數據  
                    case HSSFCell.CELL_TYPE_NUMERIC:  
                        System.out.println(cell.getNumericCellValue());  
                        break;  
                    case HSSFCell.CELL_TYPE_STRING:  
                        System.out.println(cell.getStringCellValue());  
                        break;  
                    case HSSFCell.CELL_TYPE_BOOLEAN:  
                        System.out.println(cell.getBooleanCellValue());  
                        break;  
                    case HSSFCell.CELL_TYPE_FORMULA:  
                        System.out.println(cell.getCellFormula());  
                        break;  
                    default:  
                        System.out.println("unsuported sell type");  
                    break;  
                    }  
                }  
            }  
        } catch (IOException ex) {  
            ex.printStackTrace();  
        }  
    }  
}  
參考自http://blog.csdn.net/shuwei003/article/details/6741649
D. java poi導出excel要雙擊才顯示換行
對於Java POI,其提供的API中,沒有提供直接設置單元格「折行表示」的屬性或者方法。
我之前做這個地方的時候,是利用讀取Excel的模板來實現的。
在模板文件中,對單元格設置好「折行表示」。
Java POI調用之後,先讀取模板文件中已經設置好「折行表示」的單元格的style。
然後在輸出Excel的文件中,對需要有「折行表示」的單元格,將這個style賦給它。
這樣就在最終生成的Excel看到折行表示的效果了。
E. java 用POI處理比較大的word和excel文檔。
java的主要有兩種方法
1、使用POI,這樣的包,直接的是根據office文件的相應規則,進行解析封裝
2、使用jcob這樣的,通過調用office的dll文件,操作
第一種優點:
不需要在環境上安裝office,純java環境,開發文檔較全,使用方便
缺點:
對於office的某些功能,特性兼容不是很全面,但一般的內容也可以滿足
第二種優點:
對於office文件的兼容性良好,和使用word,excel的操作基本上是一樣的
缺點:
由於是通過java調用office來完成,所以需要在環境上安裝office,文檔較少