-
大小: 10.52MB文件类型: .zip金币: 1下载: 0 次发布日期: 2023-10-07
- 语言: Java
- 标签: uploadExcel
资源简介
上传本地文件到服务器进行解析,通过poi继续操作数据,易懂简单。
代码片段和文件信息
package com;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
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 Upload {
// 判断excel版本
static Workbook openWorkbook(InputStream inString filename)throws IOException
{
Workbook wb = null;
if(filename.endsWith(“.xlsx“)){
wb = new XSSFWorkbook(in);//Excel 2007
} else {
wb = new HSSFWorkbook(in);//Excel 2003
}
return wb;
}
public void getExcelData(String fileName) throws Exception
{
InputStream in = new FileInputStream(fileName);
Workbook wb = openWorkbook(in fileName);
Sheet sheet = (Sheet)wb.getSheetAt(0);
Row row = null;
Cell cell = null;
int totalRows = sheet.getPhysicalNumberOfRows();
int totalCells = sheet.getRow(0).getPhysicalNumberOfCells();
for(int r=0; r {
row = sheet.getRow(r);
System.out.print(“第“ + r + “行“);
for(int c = 0; c < totalCells; c++)
{
cell = row.getCell(c);
String cellValue = ““;
if(null != cell){
// 以下是判断数据的类型
switch (cell.getCellType())
{
case HSSFCell.CELL_TYPE_NUMERIC: // 数字
cellValue = cell.getNumericCellValue() + ““;
// 时间格式
if(HSSFDateUtil.isCellDateFormatted(cell)){
Date dd = cell.getDateCellValue();
DateFormat df = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss“);
cellValue = df.format(dd);
}
break;
case HSSFCell.CELL_TYPE_STRING: // 字符串
cellValue = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean
cellValue = cell.getBooleanCellValue() + ““;
break;
case HSSFCell.CELL_TYPE_FORMULA: // 公式
cellValue = cell.getCellFormula() + ““;
break;
case HSSFCell.CELL_TYPE_BLANK: // 空值
cellValue = ““;
break;
case HSSFCell.CELL_TYPE_ERROR: // 故障
cellValue = “非法字符“;
break;
default:
cellValue = “未知类型“;
break;
}
System.out.print(“ “+cellValue+“\t“);
}
}
System.out.println();
}
}
public static void main(String[] args) throws Exception
{
String fileName = “C:/test.xls“;
//String fileName = “D:/text.xlsx“;
Upload upload = new Upload();
upload.getExcelData(fileName);
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2014-02-15 14:57 uploadExcel\
文件 843 2014-02-15 14:31 uploadExcel\.classpath
文件 387 2014-02-15 14:28 uploadExcel\.project
目录 0 2014-02-15 14:57 uploadExcel\bin\
目录 0 2014-02-15 14:57 uploadExcel\bin\com\
文件 3657 2014-02-15 14:56 uploadExcel\bin\com\Upload.class
文件 313898 2014-02-15 14:30 uploadExcel\dom4j-1.6.1.jar
文件 358180 2014-02-15 14:30 uploadExcel\log4j-1.2.13.jar
文件 1951279 2014-02-15 14:30 uploadExcel\poi-3.10-FINAL-20140208.jar
文件 312839 2014-02-15 14:30 uploadExcel\poi-examples-3.10-FINAL-20140208.jar
文件 30530 2014-02-15 14:30 uploadExcel\poi-excelant-3.10-FINAL-20140208.jar
文件 1031421 2014-02-15 14:30 uploadExcel\poi-ooxm
文件 4946671 2014-02-15 14:30 uploadExcel\poi-ooxm
文件 1240093 2014-02-15 14:30 uploadExcel\poi-scratchpad-3.10-FINAL-20140208.jar
目录 0 2014-02-15 14:57 uploadExcel\src\
目录 0 2014-02-15 14:57 uploadExcel\src\com\
文件 2833 2018-11-16 14:46 uploadExcel\src\com\Upload.java
文件 2666695 2014-02-15 14:30 uploadExcel\xm
- 上一篇:unity语音识别并控制任务作出相应动作
- 下一篇:图灵智能聊天机器人
评论
共有 条评论