展开全部
public class readexcel3
{
public static String name=""; //接收excel第一列
public static String phone =""; //接收excle第二列
public static void read(InputStream inputStream) throws IOException{
e5a48de588b662616964757a686964616f31333330326135
HSSFWorkbook workbook=new HSSFWorkbook(inputStream);
for(int sheetIndex=0;sheetIndex
HSSFSheet sheet=workbook.getSheetAt(sheetIndex);
for(int rowIndex=0;rowIndex<=sheet.getLastRowNum();rowIndex++){
HSSFRow row=sheet.getRow(rowIndex);
if(row==null){
continue;
}
for(int cellnum=0;cellnum<=row.getLastCellNum();cellnum++){
HSSFCell cell=row.getCell(cellnum);
getCellValue(cell, rowIndex, cellnum);
}
studentimpl.save(name, phone);
}
}
}
//判断cell的类型
public static void getCellValue(HSSFCell cell,int rowIndex,int cellnull){
if(cell==null){
return;
}else if(cell.getCellType()==HSSFCell.CELL_TYPE_BLANK){
System.out.println("第"+(rowIndex+1)+"行,第"+(cellnull+1)+"列,cellType为:CELL_TYPE_BLANK");
}else if(cell.getCellType()==HSSFCell.CELL_TYPE_STRING){
name =String.valueOf(cell.getRichStringCellValue());
// getNumericCellValue()会回传double值,若不希望出现小数点,请自行转型为int
String ce =getValue(name.toString());
System.out.println("第"+(rowIndex+1)+"行,第"+(cellnull+1)+"列,cellType为:CELL_TYPE_STRING 值为:"+ce);
}else if(cell.getCellType()==HSSFCell.CELL_TYPE_NUMERIC){
String ce = String.valueOf(cell.getNumericCellValue());
// getNumericCellValue()会回传double值,若不希望出现小数点,请自行转型为int
phone =getValue(ce.toString());
System.out.println("第"+(rowIndex+1)+"行,第"+(cellnull+1)+"列,cellType为:CELL_TYPE_NUMERIC 值为:"+phone);
}else if(cell.getCellType()==HSSFCell.CELL_TYPE_BOOLEAN){
System.out.println("第"+(rowIndex+1)+"行,第"+(cellnull+1)+"列,cellType为:CELL_TYPE_BOOLEAN 值为:"+cell.getBooleanCellValue());
}else if(cell.getCellType()==HSSFCell.CELL_TYPE_FORMULA){
System.out.println("第"+(rowIndex+1)+"行,第"+(cellnull+1)+"列,cellType为:CELL_TYPE_FORMULA 值为:"+cell.getNumericCellValue()+",公式为"+cell.getCellFormula());
}
return;
}
//将科学计数法还原【正则表达式】
public static String getValue(String account) {
String regex = "^((\\d+.?\\d+)[Ee]{1}(\\d+))$";
Pattern pattern = Pattern.compile(regex);
Matcher m = pattern.matcher(account);
boolean b = m.matches();
if (b) {
DecimalFormat df = new DecimalFormat("#");
account = df.format(Double.parseDouble(account));
}
return account;
}
public static void main(String[] args)
{
try
{
InputStream inputStream=new FileInputStream(new File("D:\\副本.xls"));
readexcel3.read(inputStream);
} catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这是读取的,将读取的数据提交的数据库中
public static void write(OutputStream outputStream) throws IOException{
HSSFWorkbook workbook=new HSSFWorkbook();
studentimpl si=new studentimpl();
List list=si.excelquery();
HSSFSheet sheet=workbook.createSheet("haha");
for(int rowIndx=0;rowIndx
HSSFRow row=sheet.createRow(rowIndx);
student stu=list.get(rowIndx);
for(int cellIndex=0;cellIndex<5;cellIndex++){
HSSFCell cell=row.createCell(cellIndex);
switch (cellIndex)
{
case 0:
cell.setCellValue(stu.getId());
break;
case 1:
cell.setCellValue(stu.getName());
break;
case 2:
cell.setCellValue(stu.getNo());
break;
case 3:
cell.setCellValue(stu.getNatives());
break;
case 4:
cell.setCellValue(stu.getEdu());
break;
}
}
}
workbook.write(outputStream);
}
public static void main(String[] args)
{
OutputStream outputStream = null;
try
{
outputStream = new FileOutputStream(new File("D:\\helssslopoi.xls"));
sqlwritexcel.write(outputStream);
// writexcel.write(outputStream);
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
} finally
{
if (outputStream != null)
{
try
{
outputStream.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
}
这是随便写的一个创建excel文件,希望对你有帮助
已赞过
已踩过<
你对这个回答的评价是?
评论
收起