JXL操作excel总结(2)

本文介绍了一个使用 Java 进行 Excel 文件读写的程序示例。该程序能够读取指定目录下的 Excel 文件,从文件中提取时间、位置和数据等信息,并将这些数据插入到 MySQL 数据库中。

2.       编写操作excel的程序:

package org.mingyuan;

import java.io.File;

import java.io.IOException;

import java.sql.*;

import java.util.Iterator;

import java.util.List;

import jxl.Cell;

import jxl.Sheet;

import jxl.Workbook;

import jxl.write.Label;

import jxl.write.WritableSheet;

import jxl.write.WritableWorkbook;

/**

* 本程序用来读取excel文件中的数据,将之处理后写入新的excel文件中

*

* @author mingyuan

*

*/

public class ReadAndWriteExcel {

       public void readExcel() throws Exception {

              Workbook wb = null;

              // 构造Workbook(工作薄)对象

              // File file = new File("G:/080628-quan-ptz01-raw data export/q.xls");

              Class.forName("com.mysql.jdbc.Driver");

              Connection conn = DriverManager

                            .getConnection("jdbc:mysql://localhost:3306/test?user=root&password=mingyuan_1986");

              PreparedStatement ps = conn

                            .prepareStatement("insert into excel(time,location,data)values(?,?,?)");

              File dir = new File("G:/a/");

              int rows = 1;// 行数统计器

              if (dir.isDirectory()) {

                     File[] files = dir.listFiles();

                     for (int j = 0; j < files.length; j++) {

                            if (files[j].getName().endsWith("xls")) {

                                   wb = Workbook.getWorkbook(files[j]);

                                   // 获得了Workbook对象之后,就可以通过它得到Sheet(工作表)对象了

                                   Sheet sheet = wb.getSheet(0);

                                   int count = sheet.getRows();

                                   double time = 0;

                                   String location = null;

                                   int data = 0;

                                   Cell cell = null;

                                   boolean isnull = false;

                                   for (int i = 1; i < count; i++) {

                                          int flag = 0;

                                          cell = sheet.getCell(1, i);

                                          if (cell.getContents().equals("")) {

                                                 isnull = true;

                                                 flag = 1;

                                          } else {

                                                 time = Double.valueOf(cell.getContents());

                                                 // System.out.print("time:" + cell.getContents());

                                          }

                                          cell = sheet.getCell(4, i);

                                          if (cell.getContents().equals("")) {

                                                 isnull = true;

                                                 flag = 1;

                                          } else {

                                                 location = new String(cell.getContents().getBytes(

                                                               "utf8"));

                                                 // /System.out.println();

                                                 // System.out.print(" location:" +

                                                 // cell.getContents());

                                          }

                                          cell = sheet.getCell(5, i);

                                          if (cell.getContents().equals("")) {

                                                 isnull = true;

                                                 flag = 1;

                                          } else {

                                                 data = Integer.valueOf(cell.getContents());

                                                 // System.out.println(" data:" +

                                                 // cell.getContents());

                                          }

                                          // System.out.println("----------"+isnull);

                                          if (flag == 0) {

                                                 try {

                                                        ps.setDouble(1, time);

                                                        ps.setString(2, location);

                                                        ps.setInt(3, data);

                                                        ps.execute();

                                                        System.out.println(rows++);

                                                 } catch (SQLException e) {

                                                        e.printStackTrace();

                                                        e.getLocalizedMessage();

                                                 } finally {

                                                 }

                                          } else {

                                                 System.out.println("--------");

                                          }

                                   }

                                   wb.close();

                                   System.out.println("OK");

                            }

                     }

              }

              try {

                     ps.close();

                     conn.close();

              } catch (SQLException e) {

                     e.getLocalizedMessage();

              }

       }

//程序未完,见(3)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值