jxl源碼
⑴ java中怎樣從Excel中讀寫數據
JavaEXCELAPI簡介
JavaExcel是一開放源碼項目,通過它Java開發人員可以讀取Excel文件的內容、創建新的Excel文件、更新已經存在的Excel文件。使用該API非Windows操作系統也可以通過純Java應用來處理Excel數據表。因為是使用Java編寫的,所以我們在Web應用中可以通過JSP、Servlet來調用API實現對Excel數據表的訪問。
應用示例
從Excel文件讀取數據表
Java ExcelAPI既可以從本地文件系統的一個文件(.xls),也可以從輸入流中讀取Excel數據表。讀取Excel數據表的第一步是創建Workbook(術語:工作薄),下面的代碼片段舉例說明了應該如何操作: 需要用到一個開源的jar包,jxl.jar。
Filefile=newFile("c:\a.xls");
InputStreamin=newFileInputStream(file);
Workbookworkbook=Workbook.getWorkbook(in);
//獲取第一張Sheet表
Sheetsheet=workbook.getSheet(0);
//我們既可能通過Sheet的名稱來訪問它,也可以通過下標來訪問它。如果通過下標來訪問的話,要注意的一點是下標從0開始,就像數組一樣。
//獲取第一行,第一列的值
Cellc00=rs.getCell(0,0);
Stringstrc00=c00.getContents();
//獲取第一行,第二列的值
Cellc10=rs.getCell(1,0);
Stringstrc10=c10.getContents();
//我們可以通過指定行和列得到指定的單元格Cell對象
Cellcell=sheet.getCell(column,row);
//也可以得到某一行或者某一列的所有單元格Cell對象
Cell[]cells=sheet.getColumn(column);
Cell[]cells2=sheet.getRow(row);
//然後再取每一個Cell中的值
Stringcontent=cell.getContents();
⑵ java程序 求抓包程序源代碼!!
package com.daidai.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static SessionFactory sessionFactory;
public static SessionFactory getSessionFactory() {
Configuration conf=new Configuration().configure();
sessionFactory=conf.buildSessionFactory();
return sessionFactory;
}
public static void setSessionFactory(SessionFactory sessionFactory) {
HibernateUtil.sessionFactory = sessionFactory;
}
public static Session getSession(){
return sessionFactory.openSession();
}
}
package com.daidai.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class Util {
private static String Cellphone_Number;
private static String Cellphone_Number1;
private static String Cellphone_Number2;
private static String Fasten_Telephone;
private static String Fasten_Telephone2;
private static String name;
private static String remarks;
public static Sheet rs;
public Util(){}
static{
try{
Workbook rwb=Workbook.getWorkbook(new File("Book1.xls"));
rs=rwb.getSheet(0);
int count=rs.getColumns();
System.out.println(count);
int count1=rs.getRows();
System.out.println(count1);
//獲取第一行,第一列的值
//count1行
//count列
for(int i=0;i<count1;i++){
for(int j=0;j<count;j++){
Cell c00 = rs.getCell(j, i);
String strc00 = c00.getContents();
//獲取第一行,第二列的值
Cell c10 = rs.getCell(1, 0);
String strc10 = c10.getContents();
System.out.println(strc00);
System.out.println(strc10);
}
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[]args){
Util u=new Util();
}
}
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class"
>com.mysql.jdbc.Driver</property>
<property name="connection.url"
>jdbc:mysql://127.0.0.1:3306/test</property>
<property name="connection.username"
>root</property>
<property name="connection.password"
>daidai</property>
<!-- SQL dialect 資料庫方言-->
<property name="dialect"
>org.hibernate.dialect.MySQLDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping resource="User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.daidai.entity">
<class name="User" table="t_user">
<id name="id">
<generator class="native"/>
</id>
<property name="name" type="java.lang.String" length="15"/>
<property name="Cellphone_Number" type="java.lang.String" length="11"/>
<property name="Cellphone_Number1" type="java.lang.String" length="11"/>
<property name="Cellphone_Number2" type="java.lang.String" length="11"/>
<property name="Fasten_Telephone" type="java.lang.String" length="11"/>
<property name="Fasten_Telephone2" type="java.lang.String" length="11"/>
<property name="remarks" type="java.lang.String"/>
</class>
</hibernate-mapping>
package com.daidai..impl;
import com.daidai.util.*;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import com.daidai..UserDAO;
import com.daidai.entity.User;
public class UserDAOImpl implements UserDAO {
public Session session=null;
public void save(User user) {
try {
session=HibernateUtil.getSession();
session.getTransaction().begin();
session.save(user);
session.getTransaction().commit();
} catch (HibernateException e) {
session.getTransaction().rollback();
e.printStackTrace();
throw e;
}
}
}
package com.daidai.;
import com.daidai.entity.User;
public interface UserDAO {
public void save(User user);
}