package com.topthinking.tel.test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
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;
import com.topthinking.tel.javabean.LinkMan;
public class ReadExcel {
public static void main(String[] args) throws IOException
{
read(); //讀取
output(); //輸出
}
public static void read() throws FileNotFoundException, IOException{
String fileToBeRead="f://135.xls";
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(fileToBeRead));
HSSFSheet sheet = workbook.getSheet("MyReport");
int rows = sheet.getPhysicalNumberOfRows();
System.out.println("測試共有行數:"+rows);
for(short r = 0 ; r < rows ; r ++){
System.out.println("這是第"+r+"行");
HSSFRow row = sheet.getRow(r); //獲取第r行
if(row != null){
int cellNum = row.getPhysicalNumberOfCells();
System.out.println("該行有"+cellNum+"列");
for(short t = 0 ; t < cellNum ; t ++){
HSSFCell cell = row.getCell(t);
String value ="";
switch (cell.getCellType())
{
case HSSFCell.CELL_TYPE_FORMULA :
//strCell = String.valueOf(aCell.getNumericCellValue());
//returnstr+=strCell+" ";
break;
case HSSFCell.CELL_TYPE_NUMERIC:
value += (long)cell.getNumericCellValue();
break;
case HSSFCell.CELL_TYPE_STRING:
value += cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_BLANK://blank
//strCell = aCell.getStringCellValue();
//returnstr+=strCell+" ";
break;
default:
value ="" ;
}
System.out.println(value);
}
}
}
}
public static void output() throws IOException{
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
// 從表中建行号,从0开始计算:第一行
HSSFRow row = sheet.createRow((short)0);
// 给工作表前8列定义列宽
sheet.setColumnWidth((short)0,(short)4000);
sheet.setColumnWidth((short)1,(short)4000);
sheet.setColumnWidth((short)2,(short)4000);
sheet.setColumnWidth((short)3,(short)8000);
// 建立单元格數組
HSSFCell cell[] = new HSSFCell[4];
// 給第一行建立(cell.length)個單元格。
for(short t = 0 ; t < cell.length; t++){
cell[t] = row.createCell((short)t);
cell[t].setEncoding(HSSFCell.ENCODING_UTF_16);//設置編碼,爲了處理中文
}
// 寫標題
cell[0].setCellValue("連絡人");
cell[1].setCellValue("聯絡人號碼");
cell[2].setCellValue("28TEL號");
cell[3].setCellValue("Email");
// 加入數據
ArrayList al = new ArrayList();
LinkMan lm1 = new LinkMan();
lm1.setName("name1");
lm1.setPhoneNum("349853u49");
lm1.setTel28("34533452828");
lm1.setEmail("xugang@126.com");
LinkMan lm2 = new LinkMan();
lm2.setName("name2");
lm2.setPhoneNum("22222!349853u49");
lm2.setTel28("22222!34533452828");
lm2.setEmail("222221xugang@126.com");
al.add(lm2);
al.add(lm1);
for( short t = 0 ; t < al.size(); t ++){
HSSFRow daterow = sheet.createRow((short)t+1);
HSSFCell datacell[] = new HSSFCell[4];
for(short i = 0 ; i < cell.length; i++){
datacell[i] = daterow.createCell((short)i);
datacell[i].setEncoding(HSSFCell.ENCODING_UTF_16);//設置編碼,爲了處理中文
}
datacell[0].setCellValue( ((LinkMan)al.get(t)).getName() );
datacell[1].setCellValue( ((LinkMan)al.get(t)).getPhoneNum() );
datacell[2].setCellValue( ((LinkMan)al.get(t)).getTel28() );
datacell[3].setCellValue( ((LinkMan)al.get(t)).getEmail() );
}
// 写入输出结果
FileOutputStream out = new FileOutputStream("F:/111.xls");
wb.write(out);
out.close();
}
public void simple(){
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
//创建一个新的行,添加几个单元格。
//行号从0开始计算
HSSFRow row = sheet.createRow((short)0);
//创建一个单元格,设置单元格的值
HSSFCell cell = row.createCell((short)0);
cell.setCellValue(1); //第一個例子
row.createCell((short)1).setCellValue(1.2); //第2個例子
HSSFCell cell1 = row.createCell((short)2); //第3個例子
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
cell1.setCellValue("一个字符串值");
row.createCell((short)3).setCellValue(true);//第4個例子
}
}
运用POI操作Excel表格实例
最新推荐文章于 2023-03-13 15:29:38 发布