您的位置:首页 > 其它

人生第一次换号

2008-03-14 11:02 260 查看
在网上找了很多很多很多,大部分都一样,都是一些人转载的,大部分都是设置什么格式的,个人水平不高,看不懂,不实用。自己写一个,简单易懂,好测试

import java.io.File;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

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 com.becoda.eca.vo.ItemExtendImport;

public class ExcelReaderAndWriter {

public static String name;
public static String sex;
public static String nationality;
public static String marriage;
public static String idCardType;
public static String idcardNo;
public static String phone;
public static String communityName;
// 设置cell编码解决中文高位字节截断
private static short XLS_ENCODING = HSSFCell.ENCODING_UTF_16;
// 定制浮点数格式
private static String NUMBER_FORMAT = "#,##0.00";
// 定制日期格式
private static String DATE_FORMAT = "m/d/yy"; // "m/d/yy h:mm"
private OutputStream out = null;
private HSSFWorkbook workbook = null;
private HSSFSheet sheet = null;
private HSSFRow row = null;
static List<ItemExtendImport> list = new ArrayList<ItemExtendImport>();

static void ReadExcel(String fileFrom){
try {
ItemExtendImport itemExtendImport = new ItemExtendImport();
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(fileFrom));
HSSFSheet sheet = workbook.getSheetAt(0);
int countRow = sheet.getLastRowNum();
for (int i = 1; i <= countRow; i++) {
HSSFRow row = sheet.getRow(i);
HSSFCell cell = null;
cell = row.getCell((short) 0);
name = (cell != null) ? cell.getRichStringCellValue().toString() : "";
cell = row.getCell((short) 1);
sex = (cell != null) ? cell.getRichStringCellValue().toString() : "";
cell = row.getCell((short) 2);
nationality = (cell != null) ? cell.getRichStringCellValue().toString() : "";
cell = row.getCell((short) 3);
marriage = (cell != null) ? cell.getRichStringCellValue().toString() : "";
cell = row.getCell((short) 4);
idCardType = (cell != null) ? cell.getRichStringCellValue().toString() : "";
cell = row.getCell((short) 5);
idcardNo = (cell != null) ? cell.getRichStringCellValue().toString() : "";
cell = row.getCell((short) 6);
phone = (cell != null) ? cell.getRichStringCellValue().toString() : "";
cell = row.getCell((short) 7);
communityName = (cell != null) ? cell.getRichStringCellValue().toString() : "";
itemExtendImport.setName(name);
itemExtendImport.setSex(sex);
itemExtendImport.setNationality(nationality);
itemExtendImport.setMarriage(marriage);
itemExtendImport.setIdCardType(idCardType);
itemExtendImport.setIdcardNo(idcardNo);
itemExtendImport.setPhone(phone);
itemExtendImport.setCommunityName(communityName);
list.add(i-1, itemExtendImport);
ItemExtendImport itemExtend = list.get(i-1);
System.out.println(itemExtend.getName());
System.out.println();
}
} catch (Exception e) {
System.out.println("运行中出错了---: " + e);
}
}

public ExcelReaderAndWriter() {
}

/**
* 初始化Excel
*/
public ExcelReaderAndWriter(OutputStream out) {
this.out = out;
this.workbook = new HSSFWorkbook();
this.sheet = workbook.createSheet();
}

/**
* 导出Excel文件
* @throws IOException
*/
public void export() throws FileNotFoundException, IOException {
try {
workbook.write(out);
out.flush();
out.close();
} catch (FileNotFoundException e) {
throw new IOException(" 生成导出Excel文件出错! ", e);
} catch (IOException e) {
throw new IOException(" 写入Excel文件出错! ", e);
}
}

/**
* 增加一行
*/
public void createRow(int index) {
this.row = this.sheet.createRow(index);
}

/**
* 获取单元格的值
*/
public String getCell(int index) {
HSSFCell cell = this.row.getCell((short) index);
String strExcelCell = "";
if (cell != null) {
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_FORMULA:
strExcelCell = "FORMULA ";
break;
case HSSFCell.CELL_TYPE_NUMERIC: {
strExcelCell = String.valueOf(cell.getNumericCellValue());
}
break;
case HSSFCell.CELL_TYPE_STRING:
strExcelCell = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_BLANK:
strExcelCell = "";
break;
default:
strExcelCell = "";
break;
}
}
return strExcelCell;
}

/**
* 设置单元格
* @param index 列号
* @param value 单元格填充值
*/
public void setCell(int index, int value) {
HSSFCell cell = this.row.createCell((short) index);
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue(value);
}

/**
* 设置单元格
* @param index 列号
* @param value 单元格填充值
*/
public void setCell(int index, double value) {
HSSFCell cell = this.row.createCell((short) index);
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue(value);
HSSFCellStyle cellStyle = workbook.createCellStyle(); // 建立新的cell样式
HSSFDataFormat format = workbook.createDataFormat();
cellStyle.setDataFormat(format.getFormat(NUMBER_FORMAT)); // 设置cell样式为定制的浮点数格式
cell.setCellStyle(cellStyle); // 设置该cell浮点数的显示格式
}

/**
* 设置单元格
* @param index 列号
* @param value 单元格填充值
*/
public void setCell(int index, String value) {
HSSFCell cell = this.row.createCell((short) index);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(XLS_ENCODING);
cell.setCellValue(value);
}

/**
* 设置单元格
* @param index 列号
* @param value 单元格填充值
*/
public void setCell(int index, Calendar value) {
HSSFCell cell = this.row.createCell((short) index);
cell.setEncoding(XLS_ENCODING);
cell.setCellValue(value.getTime());
HSSFCellStyle cellStyle = workbook.createCellStyle(); // 建立新的cell样式
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat(DATE_FORMAT)); // 设置cell样式为定制的日期格式
cell.setCellStyle(cellStyle); // 设置该cell日期的显示格式
}

public static void main(String args[]) {
String fileName = "D:\\test.xls";
ReadExcel(fileName);
System.out.println();

System.out.println(" 开始导出Excel文件... ");
File f = new File("C:\\qt.xls");
ExcelReaderAndWriter e = new ExcelReaderAndWriter();
try {
e = new ExcelReaderAndWriter(new FileOutputStream(f));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}

e.createRow(0);
e.setCell(0, "姓名");
e.setCell(1, "性别");
e.setCell(2, "民族");
e.setCell(3, "婚姻");
e.setCell(4, "证件类型");
e.setCell(5, "证件号");
e.setCell(6, "电话");
e.setCell(7, "居住地");

for(int i = 1; i <= list.size();i++){
ItemExtendImport itemExtendImport = list.get(i-1);
e.createRow(i);
e.setCell(0, itemExtendImport.getName());
e.setCell(1, itemExtendImport.getSex());
e.setCell(2, itemExtendImport.getNationality());
e.setCell(3, itemExtendImport.getMarriage());
e.setCell(4, itemExtendImport.getIdCardType());
e.setCell(5, itemExtendImport.getIdcardNo());
e.setCell(6, itemExtendImport.getPhone());
e.setCell(7, itemExtendImport.getCommunityName());
}
try {
e.export();
System.out.println(" 导出Excel文件[成功] ");
} catch (IOException ex) {
System.out.println(" 导出Excel文件[失败] ");
ex.printStackTrace();
}
}
}本文出自 “踏雪寻春” 博客,请务必保留此出处http://snowboy.blog.51cto.com/5848432/1009618
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: