站内搜索: 请输入搜索关键词

当前页面: 开发资料首页JSP 专题poi操作Excel的问题,总也有问题,谁能给个例子看看。

poi操作Excel的问题,总也有问题,谁能给个例子看看。

摘要: poi操作Excel的问题,总也有问题,谁能给个例子看看。


poi操作Excel的问题,总也有问题,谁能给个例子看看。


写入 还是 导出
最好是找出 自己得问题 给个 例子网上很多得


写入:


package excel;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.DecimalFormat;
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 POIMain {
public static String outputFile = "e://test//tels.xls";
public static String fileToBeRead = "e://test//tels.xls";
public void CreateExcel() {
try {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");

// Create a row and put some cells in it. Rows are 0 based.
HSSFRow row;
HSSFCell cell;
for (int i=0;i<5;i++)
{
row = sheet.createRow((short)i);
// Create a cell and put a value in it.
cell = row.createCell((short)0);
cell.setCellValue(1);

// Or do it on one line.
row.createCell((short)1).setCellValue(1.2);
row.createCell((short)2).setCellValue("This is a string");
row.createCell((short)3).setCellValue(true);
}


// Write the output to a file
FileOutputStream fileOut = new FileOutputStream(outputFile);
wb.write(fileOut);
fileOut.close();

} catch (Exception e) {
System.out.println("已运行 xlCreate() : " + e);
}
}
public static void main(String[] args) {
POIMain poi = new POIMain();
poi.CreateExcel();
}
}




读取
public class GetExcelValue
{
private String excelfile;//导入excel文件的名称和路径
// private short ReportValueLength;
private int RecordCount;
private double[] ReportValue;//=new double[100];//返回excel文件的各个单元格内容到数组
private String Message;//出现错误时返回的错误消息
private int ReturnCode;//调用的返回值,0为正常,1为异常
public GetExcelValue(String FileName,int ReportLength)
{
excelfile=FileName;
//初始化返回变量
Message="";
ReturnCode=0;
RecordCount=0;
ReportValue=new double[ReportLength];
}
public void OutPutExcelValue()//取得excel报表的内容
{
try
{
// 创建对Excel工作簿文件的引用
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(excelfile));
// .println("===SheetsNum===" + workbook.getNumberOfSheets());//获取sheet数
for (int numSheets = 0; numSheets < workbook.getNumberOfSheets(); numSheets++)
{
if (null != workbook.getSheetAt(numSheets))
{
HSSFSheet aSheet = workbook.getSheetAt(numSheets);//获得一个sheet
//System.out.println("+++getFirstRowNum+++" +
// aSheet.getFirstRowNum());//
//System.out.println("+++getLastRowNum+++" +
// aSheet.getLastRowNum());取得一个sheet的行数,注意行数从0开始计数
// RecordCount=aSheet.getLastRowNum()+1;
for (int rowNumOfSheet = 0; rowNumOfSheet <= aSheet.getLastRowNum(); rowNumOfSheet++)
{
if (null != aSheet.getRow(rowNumOfSheet))
{
HSSFRow aRow = aSheet.getRow(rowNumOfSheet);
//System.out.println(">>>getFirstCellNum<<<"+
// aRow.getFirstCellNum());
//System.out.println(">>>getLastCellNum<<<"+
// aRow.getLastCellNum());取得一行的单元割格数,注意行数从1开始计数
for (short cellNumOfRow = 0; cellNumOfRow <= (aRow.getLastCellNum()-1); cellNumOfRow++)
{

if (null != aRow.getCell(cellNumOfRow))
{
HSSFCell aCell = aRow.getCell(cellNumOfRow);
int cellType = aCell.getCellType();
//System.out.println(cellType);
switch (cellType)
{
case 0://Numeric,CELL_TYPE_NUMERIC
String CheckLength=String.valueOf(aCell.getNumericCellValue());
if(CheckLength.length()>18)
{
Message="错误,第"+(rowNumOfSheet+1)+"行"+(cellNumOfRow+1)+"列"+"单元格的内容超过最大长度";
ReturnCode=1;
return;
}
ReportValue[cellNumOfRow]= aCell.getNumericCellValue();
Message=String.valueOf(ReportValue[cellNumOfRow]);
//System.out.println(strCell);
break;
case 1://String
String strCell = aCell.getStringCellValue();
String tempstr=strCell.trim();
if(tempstr.length()==0)
{
ReportValue[cellNumOfRow]=0;
}
else
{
Message="错误,第"+(rowNumOfSheet+1)+"行"+(cellNumOfRow+1)+"列"+"单元格的内容不对,注意只可以是数字或空格或不输入!";
ReturnCode=1;
return;
}
//System.out.println(strCell);
break;
case 3:
ReportValue[cellNumOfRow]=0;
break;
default:
Message="错误,第"+(rowNumOfSheet+1)+"行"+(cellNumOfRow+1)+"列"+"单元格的内容不对,注意只可以是数字或空格或不输入!";
ReturnCode=1;
return;
//System.out.println("格式不对不读");//其它格式的数据
}
}
}
}
RecordCount=rowNumOfSheet+1;//取得成功处理的数据条数
}
}
}
// System.out.print("aaa");
Message="成功处理了"+RecordCount+"条数据";
}
catch (FileNotFoundException e)
{
Message="错误,未找到指定文件";
ReturnCode=1;
}
catch (Exception e)
{
ReturnCode=1;
Message=e.toString();
e.printStackTrace();
// System.out.println("ReadExcelError" + e);
}
/*
finally
{
//在无论出错还是不出错都执行的代码!
Message="打开文件失败!";
// Message=
ReturnCode=1;
}*/
}
public double[] getReportValue()
{
// Message=String.valueOf(ReportValue[2]);
return ReportValue;
}
public String getMessage()
{
return Message;
}
public int getReturnCode()
{
return ReturnCode;
}
}


ding


↑返回目录
前一篇: 求助,关于ResultSet is read only的异常。
后一篇: 请教大家一句MSSQL语句的写法,必解贴.具体我表达不太清楚,请大家看看我写出的例子,帮忙解决下,谢了