我在用Apache的POI组件编写一个读取Excel文件数据和生成Excel文件的模块,出现了一个很奇怪的问题,就是当我从一个Excel源文件(workbook.xml)读取数据,并将其保存在一个List中,然后我再把这个List中的数据写入一个新的Excel源文件(wrorkbook2.xml,且格式数据和workbook.xml一样),这两个功能都成功了;奇怪的是,当我编写测试程序来测试者两个功能模块时,发现我用程序读取wrorkbook2.xml文件时,最后一列的数据始终读取不了(即,读取workbook.xml时,是完整的读取了7列,但读取wrorkbook2.xml时,只能读取6列);并且,我发现wrorkbook2.xml文件(5KB)的大小比wrorkbook.xml(17KB)小很多,有熟悉POI组件的人么,解答一下,谢谢!
下面附模块代码;
package module;import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;/**
* 用于对Excel文档进行读写操作
* @author seven
*
*/public class ExcelAccess
{
/**
* 用于获取Excel文件中的数据
* @param filePath Excel文件所在路径
* @return 包含Excel文件中的数据的数组
*/
public List<String> ReadExcelDate(String filePath)
{
File excelFile = null;// Excel文件对象
InputStream is = null;// 输入流对象
List<String> result = new ArrayList<String>(); // 暂存Excel文档中的数据
try
{
excelFile = new File(filePath);
is = new FileInputStream(excelFile);// 获取文件输入流
HSSFWorkbook workbook2003 = new HSSFWorkbook(is);// 创建Excel2003文件对象
HSSFSheet sheet = workbook2003.getSheetAt(0);// 取出第一个工作表,索引是0
// 开始循环遍历行,表头不处理,从1开始
for (int rowIndex = 1; rowIndex <= sheet.getLastRowNum(); rowIndex++)
{
HSSFRow row = sheet.getRow(rowIndex);// 获取行对象
System.out.println(row.getLastCellNum());
for (int colIndex = 0; colIndex < row.getLastCellNum(); colIndex++)
{
HSSFCell cell = row.getCell((short)colIndex);// 获取单元格对象
if (cell == null) // 判断单元格是否为空
{
result.add("");
continue;
}
result.add(String.valueOf(cell.getNumericCellValue()));
}
}
}catch( FileNotFoundException ee){
ee.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally { // 关闭文件流
if (is != null) {
try
{
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
/**
* 用于向Excel文档中写入数据
* @param storeDate Excel中要保存的数据
* @param filePath Excel文档所在路径
* @param sheetName Excel文档中工作表名
* @param header Excel文档中工作表的表头
* @return 写入是否成功
* */
public boolean generateExcel(List<String> storeDate, String filePath,String sheetName,String[] header)
{
boolean flag = false; // 保存成功与否的标志
Iterator<String> iter = storeDate.iterator();
int colNum = header.length;
int rowNum = storeDate.size() / colNum; // 保存存入数据在工作表中的行数
HSSFWorkbook workbook2003 = new HSSFWorkbook(); // 先创建工作簿对象
HSSFSheet sheet = workbook2003.createSheet(sheetName); // 创建工作表对象并命名
// 创建工作表的表头
HSSFRow rowHead = sheet.createRow(0); // 创建行
for(int colIndex = 0; colIndex < colNum; colIndex++) // 开始创建单元格并赋值
{
HSSFCell nameCell = rowHead.createCell((short)colIndex); // 创建单元
HSSFRichTextString rts = new HSSFRichTextString(header[colIndex]);
nameCell.setCellValue(rts);
}
// 遍历集合对象创建行和单元格
for(int rowIndex = 1; rowIndex <= rowNum; rowIndex++)
{
String str;
HSSFRow rowBody = sheet.createRow(rowIndex); // 创建行
for(int colIndex = 0; colIndex < colNum; colIndex++) // 开始创建单元格并赋值
{
HSSFCell nameCell = rowBody.createCell((short)colIndex); // 创建单元
str = (String)iter.next();
try
{
Double doubleCell =Double.parseDouble(str);
nameCell.setCellValue(doubleCell);
}catch(NumberFormatException e){
HSSFRichTextString rts = new HSSFRichTextString(str); // 空单元格
nameCell.setCellValue(rts);
}
}
}
File file = new File(filePath);// 生成文件
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
workbook2003.write(fos); // 写入Excel文件
flag = true;
} catch (Exception e) {
e.printStackTrace();
} finally { // 关闭文件流
if (fos != null) {
try
{
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return flag;
}
}
下面附模块代码;
package module;import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;/**
* 用于对Excel文档进行读写操作
* @author seven
*
*/public class ExcelAccess
{
/**
* 用于获取Excel文件中的数据
* @param filePath Excel文件所在路径
* @return 包含Excel文件中的数据的数组
*/
public List<String> ReadExcelDate(String filePath)
{
File excelFile = null;// Excel文件对象
InputStream is = null;// 输入流对象
List<String> result = new ArrayList<String>(); // 暂存Excel文档中的数据
try
{
excelFile = new File(filePath);
is = new FileInputStream(excelFile);// 获取文件输入流
HSSFWorkbook workbook2003 = new HSSFWorkbook(is);// 创建Excel2003文件对象
HSSFSheet sheet = workbook2003.getSheetAt(0);// 取出第一个工作表,索引是0
// 开始循环遍历行,表头不处理,从1开始
for (int rowIndex = 1; rowIndex <= sheet.getLastRowNum(); rowIndex++)
{
HSSFRow row = sheet.getRow(rowIndex);// 获取行对象
System.out.println(row.getLastCellNum());
for (int colIndex = 0; colIndex < row.getLastCellNum(); colIndex++)
{
HSSFCell cell = row.getCell((short)colIndex);// 获取单元格对象
if (cell == null) // 判断单元格是否为空
{
result.add("");
continue;
}
result.add(String.valueOf(cell.getNumericCellValue()));
}
}
}catch( FileNotFoundException ee){
ee.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally { // 关闭文件流
if (is != null) {
try
{
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
/**
* 用于向Excel文档中写入数据
* @param storeDate Excel中要保存的数据
* @param filePath Excel文档所在路径
* @param sheetName Excel文档中工作表名
* @param header Excel文档中工作表的表头
* @return 写入是否成功
* */
public boolean generateExcel(List<String> storeDate, String filePath,String sheetName,String[] header)
{
boolean flag = false; // 保存成功与否的标志
Iterator<String> iter = storeDate.iterator();
int colNum = header.length;
int rowNum = storeDate.size() / colNum; // 保存存入数据在工作表中的行数
HSSFWorkbook workbook2003 = new HSSFWorkbook(); // 先创建工作簿对象
HSSFSheet sheet = workbook2003.createSheet(sheetName); // 创建工作表对象并命名
// 创建工作表的表头
HSSFRow rowHead = sheet.createRow(0); // 创建行
for(int colIndex = 0; colIndex < colNum; colIndex++) // 开始创建单元格并赋值
{
HSSFCell nameCell = rowHead.createCell((short)colIndex); // 创建单元
HSSFRichTextString rts = new HSSFRichTextString(header[colIndex]);
nameCell.setCellValue(rts);
}
// 遍历集合对象创建行和单元格
for(int rowIndex = 1; rowIndex <= rowNum; rowIndex++)
{
String str;
HSSFRow rowBody = sheet.createRow(rowIndex); // 创建行
for(int colIndex = 0; colIndex < colNum; colIndex++) // 开始创建单元格并赋值
{
HSSFCell nameCell = rowBody.createCell((short)colIndex); // 创建单元
str = (String)iter.next();
try
{
Double doubleCell =Double.parseDouble(str);
nameCell.setCellValue(doubleCell);
}catch(NumberFormatException e){
HSSFRichTextString rts = new HSSFRichTextString(str); // 空单元格
nameCell.setCellValue(rts);
}
}
}
File file = new File(filePath);// 生成文件
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
workbook2003.write(fos); // 写入Excel文件
flag = true;
} catch (Exception e) {
e.printStackTrace();
} finally { // 关闭文件流
if (fos != null) {
try
{
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return flag;
}
}
解决方案 »
- 华为smproxy SMPP 使用报错The constructor SMPPSubmitMessage(String···) is undefined
- 一道递归调用题
- JApplet无法刷新问题 急
- 获取面板的长和宽的问题,请高手指教
- 新手问个简单的问题???
- 一个关于ArrayList重新组合排序的问题
- 有关JAVA的类!!!!!
- java初学者的疑问
- Applet 静态页面显示,而在动态页面不显示的问题
- 我正忙于有关于WebGIS的毕设,其中有想在Applet中放置几个TabPage,即标签页,敬请各位师长学兄不吝赐教,小弟在此先表谢意。如由简单实例,那最好不过,我的e-mail是[email protected]
- String问题
- FrameView窗口关闭消息无法截取
1.你读取xls数据时
for (int colIndex = 0; colIndex < row.getLastCellNum(); colIndex++)
该for循环中的终止条件是colIndex <= row.getLastCellNum(); 而你的是colIndex < row.getLastCellNum();
另外我写了一个execl转为数组的工具类,可以参考下:
http://bbs.csdn.net/topics/390360927
当然程序中肯定还有会有BUG,欢迎指点批评。