乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > java读取excel文件运行问题,用的是jxl包运行时选...

java读取excel文件运行问题,用的是jxl包运行时选...

作者:乔山办公网日期:

返回目录:excel表格制作


这应该是excel文件中的问题,看看是不是文件中后面那些行里面有空格,或者做过其他标记,可以直接把excel的第21到第31行选中然后删除,保存好之后重新用jxl读取试试。

//Excel 数据 比对 系统

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class ExcelContrast {
static String file1 = "d://java//1.xls";
static String file2 = "d://java//2.xls";
static HashMap<Integer, String> hsMap1 = new HashMap<Integer, String>();
static HashMap<Integer, String> hsMap2 = new HashMap<Integer, String>();
static int found = 0;
public static void main(String[] args) {
System.out.println("欢迎使用Excel数据比对系统!");
System.out.println("Excel数据比对系统 开始运行。。。");
System.out.println("读取" + file1);
ReadExcelToHashMap(file1, hsMap1);
// System.out.println(hsMap1.size());
System.out.println("读取" + file1);
ReadExcelToHashMap(file2, hsMap2);
// System.out.println(hsMap2.size());
// System.out.println(hsMap1.values());
// System.out.println(hsMap2.values());
System.out.println("开始比对。。。");
server();
System.out.println("成功比对出" + found + "条不同数据");
System.out.println("Excel数据比对系统 正常退出。。。");
}
private static void server() {
int i = 0;
System.out.println("遍历比对" + file1);
while (i < hsMap1.size()) {
if (hsMap2.containsValue(hsMap1.get(i))) {
} else {
System.out.println(file1 + " 第" + (i + 1) + "行 "
+ hsMap1.get(i));
UpdateExcel(i, 0, hsMap1.get(i), file1);
UpdateExcel(i, 1, "稽核", file1);
found++;
}
i++;
}
i = 0;
System.out.println("遍历比对" + file2);
while (i < hsMap2.size()) {
if (hsMap1.containsValue(hsMap2.get(i))) {
} else {
System.out.println(file2 + " 第" + (i + 1) + "行 "
+ hsMap2.get(i));
UpdateExcel(i, 0, hsMap2.get(i), file2);
UpdateExcel(i, 1, "稽核", file2);
found++;
}
i++;
}
}
private static void ReadExcelToHashMap(String file,
HashMap<Integer, String> hsmap) {
try {
Workbook book = Workbook.getWorkbook(new File(file));
// 获得第一个工作表对象
Sheet sheet = book.getSheet(0);
// 得到第一列第一行的单元格
int i = 0;
while (!sheet.getCell(0, i).getContents().equalsIgnoreCase("")) {
hsmap.put(i, sheet.getCell(0, i).getContents());
i++;
}
book.close();
} catch (FileNotFoundException e) {
System.out.println("系统找不到指定的文件:" + file);
System.exit(0);
} catch (ArrayIndexOutOfBoundsException e) {
} catch (Exception e) {
e.printStackTrace();
}
}
static void test_hash() {
HashMap<Integer, String> hsMap = new HashMap<Integer, String>();
hsMap.put(3, "Value3");
hsMap.put(2, "Value2");
System.out.println(hsMap.get(2));
System.out.println(hsMap.values());
System.out.println(hsMap.containsKey(1));
System.out.println(hsMap.containsValue("Value3"));
System.out.println(hsMap.size());
}
static void UpdateExcel(int i, int j, String cell, String file) {
try {
// Excel获得文件
Workbook wb = Workbook.getWorkbook(new File(file));
// 打开一个文件的7a64e4b893e5b19e361副本,并且指定数据写回到原文件
WritableWorkbook book = Workbook.createWorkbook(new File(file), wb);
// 添加一个工作表
WritableSheet ws = book.getSheet(0);
// 这里需要注意的是,在Excel中,第一个参数表示列,第二个表示行
WritableFont font = new WritableFont(WritableFont.ARIAL, 15,
WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,
Colour.RED);
WritableCellFormat cFormat = new WritableCellFormat(font);
Label labelC = new Label(j, i, cell, cFormat);
try {
// 将生成的单元格添加到工作表中
ws.addCell(labelC);
} catch (Exception e) {
e.printStackTrace();
}
book.write();
book.close();
} catch (FileNotFoundException e) {
System.out.println("另一个程序正在使用此文件,进程无法访问。请您关闭EXECL");
System.exit(0);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Java Excel 是一个开源项目,通过它Java开发人员可以读取Excel文件的e799bee5baa6e997aee7ad94e4b893e5b19e334内容、创建新的Excel文件、更新已经存在的Excel文件等,在项目中需要导入名为jxl.jar的包。在这里只是示例它的基本用法,其他高级的功能(图片、公式、格式等)请参考Java Excel的帮助文档。

如有一个用户资料的Excel表,包含ID、用户名、性别、邮件等信息,定义一个用户JavaBean:
package com.monitor1394.excel;

/**
*
* 用户
*
* @author monitor
* Created on 2010-12-22, 9:57:58
*/
public class User {
/** ID */
private int id;
/** 用户名 */
private String name;
/** 性别 1:男 2:女*/
private int sex;
/** 邮件 */
private String email;

public User(){
}

public User(int id,String name,int sex,String email){
this.id=id;
this.name=name;
this.sex=sex;
this.email=email;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getSex() {
return sex;
}

public void setSex(int sex) {
this.sex = sex;
}

@Override
public String toString(){
return id+":"+name;
}
}
提供的Excel表操作类如下,某些单元格的格式可按自己意愿指定:
package com.monitor1394.excel;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.VerticalAlignment;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.NumberFormats;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

/**
*
* Excel表操作
*
* @author monitor
* Created on 2010-12-22, 9:50:28
*/
public class Excel {
/** 标题单元格格式 */
private static WritableCellFormat titleFormat=null;
/** 主题内容单元格格式 */
private static WritableCellFormat bodyFormat=null;
/** 注释单元格格式 */
private static WritableCellFormat noteFormat=null;
/** 浮点型数据的单元格格式 */
private static WritableCellFormat floatFormat=null;
/** 整型数据的单元格格式 */
private static WritableCellFormat intFormat=null;
/** 初始化数据 */
private static boolean init=false;

/** 私有构造方法,防止错误使用Excel类 */
private Excel(){
}

/**
* 初始化各单元格格式
* @throws WriteException 初始化失败
*/
private static void init() throws WriteException{
WritableFont font1,font2,font3,font4;
//Arial字体,9号,粗体,单元格黄色,田字边框,居中对齐
font1 = new WritableFont(WritableFont.ARIAL, 9, WritableFont.BOLD, false);
titleFormat = new WritableCellFormat (font1);
titleFormat.setBackground(Colour.YELLOW);
titleFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
titleFormat.setAlignment(Alignment.CENTRE);
//Arial字体,9号,粗体,单元格黄色,田字边框,左右居中对齐,垂直居中对齐,自动换行
font2 = new WritableFont(WritableFont.ARIAL, 9, WritableFont.BOLD, false);
noteFormat = new WritableCellFormat (font2);
noteFormat.setBackground(Colour.YELLOW);
noteFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
noteFormat.setAlignment(Alignment.CENTRE);
noteFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
noteFormat.setWrap(true);
//Arial字体,9号,非粗体,单元格淡绿色,田字边框
font3 = new WritableFont(WritableFont.ARIAL, 9, WritableFont.NO_BOLD, false);
bodyFormat = new WritableCellFormat (font3);
bodyFormat.setBackground(Colour.LIGHT_GREEN);
bodyFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
//Arial字体,9号,非粗体,单元格淡绿色,田字边框
font4 = new WritableFont(WritableFont.ARIAL, 9, WritableFont.NO_BOLD, false);
floatFormat = new WritableCellFormat (font4,NumberFormats.FLOAT);
floatFormat.setBackground(Colour.LIGHT_GREEN);
floatFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
//Arial字体,9号,非粗体,单元格淡绿色,田字边框
font4 = new WritableFont(WritableFont.ARIAL, 9, WritableFont.NO_BOLD, false);
intFormat = new WritableCellFormat (font4,NumberFormats.INTEGER);
intFormat.setBackground(Colour.LIGHT_GREEN);
intFormat.setBorder(Border.ALL, BorderLineStyle.THIN);

init=true;
}

public static void createUserExcelFile(List<User> userList,File destFile) throws WriteException, IOException{
if(init==false) init();
int index,row;
WritableSheet sheet=null;
WritableWorkbook book=null;
book = Workbook.createWorkbook(destFile);
sheet = book.createSheet("用户表", 0);
sheet.setColumnView(0, 15);
sheet.setColumnView(1, 15);
sheet.setColumnView(2, 15);
sheet.setColumnView(3, 40);
//字段变量名
index=0;
sheet.addCell(new Label(index++,0,"id",titleFormat));
sheet.addCell(new Label(index++,0,"name",titleFormat));
sheet.addCell(new Label(index++,0,"sex",titleFormat));
sheet.addCell(new Label(index++,0,"email",titleFormat));
//字段名
index=0;
sheet.addCell(new Label(index++,1,"ID",titleFormat));
sheet.addCell(new Label(index++,1,"用户名",titleFormat));
sheet.addCell(new Label(index++,1,"性别",titleFormat));
sheet.addCell(new Label(index++,1,"邮件",titleFormat));
//字段注释
index=0;
sheet.addCell(new Label(index++,2,null,noteFormat));
sheet.addCell(new Label(index++,2,null,noteFormat));
sheet.addCell(new Label(index++,2,"1:男/n2:女",noteFormat));
sheet.addCell(new Label(index++,2,null,noteFormat));
row=3;
for(User user:userList){
if(user==null) continue;
index=0;
sheet.addCell(new Number(index++,row,user.getId(),bodyFormat));
sheet.addCell(new Label(index++,row,user.getName(),bodyFormat));
sheet.addCell(new Number(index++,row,user.getSex(),bodyFormat));
sheet.addCell(new Label(index++,row,user.getEmail(),bodyFormat));
row++;
}
book.write();
if(book!=null) book.close();
}

public static List<User> readUserExcelFile(File file) throws IOException, BiffException{
if(file==null) return null;
int row,column;
String temp=null;
Workbook book =null;
Sheet sheet=null;
List<User> userList=new ArrayList<User>();
book = Workbook.getWorkbook(file);
sheet = book.getSheet(0);
row=3;
while(row<sheet.getRows()){
column=0;
User user=new User();
//id
temp=sheet.getCell(column++,row).getContents().trim();
if(temp!=null && !temp.equals("") && temp.matches("//d+")) user.setId(Integer.parseInt(temp));
else break;
//名称
temp=sheet.getCell(column++,row).getContents().trim();
if(temp!=null && !temp.equals("")) user.setName(temp);
//性别
temp=sheet.getCell(column++,row).getContents().trim();
if(temp!=null && !temp.equals("") && temp.matches("//d+")) user.setSex(Integer.parseInt(temp));
//邮件
temp=sheet.getCell(column++,row).getContents().trim();
if(temp!=null && !temp.equals("")) user.setEmail(temp);

userList.add(user);
row++;
}
if(book!=null) book.close();
return userList;
}
}
要导入的Excel表格式如下:

导出后的Excel表如下:

这个没关系,读取excel方法中也只是根据excel的行跟列来取值,跟列名没关系,只要你后台能确定你取到的值是什么就行了

相关阅读

关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网