:http://zhidao.baidu.com/question/140741029.html
-jxl.jar, http://sourceforge.net/project/showfiles.php?group_id=79926 。
. Excel
/** Excel
* @param file
* @return
*/
public static String readExcel(File file){
StringBuffer sb = new StringBuffer();
Workbook wb = null;
try {
// Workbook( )
wb=Workbook.getWorkbook(file);
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if(wb==null)
return null;
// Workbook , Sheet( )
Sheet[] sheet = wb.getSheets();
if(sheet!=null&&sheet.length>0){
//
for(int i=0;i<sheet.length;i++){
//
int rowNum = sheet[i].getRows();
for(int j=0;j<rowNum;j++){
//
Cell[] cells = sheet[i].getRow(j);
if(cells!=null&&cells.length>0){
//
for(int k=0;k<cells.length;k++){
//
String cellValue = cells[k].getContents();
sb.append(cellValue+" ");
}
}
sb.append(" ");
}
sb.append(" ");
}
}
// ,
wb.close();
return sb.toString();
}
. Excel
, , , jxl api, :http://www.ibm.com/developerworks/cn/java/l-javaExcel/?ca=j-t10
/** Excel
* @param fileName Excel
*/
public static void writeExcel(String fileName){
WritableWorkbook wwb = null;
try {
// Workbook (Workbook)
wwb = Workbook.createWorkbook(new File(fileName));
} catch (IOException e) {
e.printStackTrace();
}
if(wwb!=null){
//
//Workbook createSheet , ,
WritableSheet ws = wwb.createSheet("sheet1", 0);
//
for(int i=0;i<10;i++){
for(int j=0;j<5;j++){
// , Excel , ,
Label labelC = new Label(j, i, " "+(i+1)+" , "+(j+1)+" ");
try {
//
ws.addCell(labelC);
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
}
try {
//
wwb.write();
// ,
wwb.close();
} catch (IOException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
}
. Excel
/**
* @param file
* @param keyWord
* @return
*/
public static boolean searchKeyWord(File file,String keyWord){
boolean res = false;
Workbook wb = null;
try {
// Workbook( )
wb=Workbook.getWorkbook(file);
} catch (BiffException e) {
return res;
} catch (IOException e) {
return res;
}
if(wb==null)
return res;
// Workbook , Sheet( )
Sheet[] sheet = wb.getSheets();
boolean breakSheet = false;
if(sheet!=null&&sheet.length>0){
//
for(int i=0;i<sheet.length;i++){
if(breakSheet)
break;
//
int rowNum = sheet[i].getRows();
boolean breakRow = false;
for(int j=0;j<rowNum;j++){ /**
* @param file
* @param keyWord
* @return
*/
public static boolean searchKeyWord(File file,String keyWord){
boolean res = false;
Workbook wb = null;
try {
// Workbook( )
wb=Workbook.getWorkbook(file);
} catch (BiffException e) {
return res;
} catch (IOException e) {
return res;
}
if(wb==null)
return res;
// Workbook , Sheet( )
Sheet[] sheet = wb.getSheets();
boolean breakSheet = false;
if(sheet!=null&&sheet.length>0){
//
for(int i=0;i<sheet.length;i++){
if(breakSheet)
break;
//
int rowNum = sheet[i].getRows();
boolean breakRow = false;
for(int j=0;j<rowNum;j++){
if(breakRow)
break;
//
Cell[] cells = sheet[i].getRow(j);
if(cells!=null&&cells.length>0){
boolean breakCell = false;
//
for(int k=0;k<cells.length;k++){
if(breakCell)
break;
//
String cellValue = cells[k].getContents();
if(cellValue==null)
continue;
if(cellValue.contains(keyWord)){
res = true;
breakCell = true;
breakRow = true;
breakSheet = true;
}
}
}
}
}
}
// ,
wb.close();
return res;
}
if(breakRow)break;/ の のすべてのセルセルセルセルセルCell=sheet[i].getRow(j);if(cell!=null&cell.length>0){bollan breakCell=false;// セルを for(int k=0;k<セル.lengthk+){if(breakCell)break;// のセルの String cell Value=cels[k].gets Contents();if(cell Value==null)contine;if(cell Value.com ntains){res=true;breakCell=true;breakRow=true;break Sheet=true;}// にリソースを じてメモリを します。return ress;; . Excel
, :
/** Excel
* @param dataSheet
* @param col
* @param row
* @param width
* @param height
* @param imgFile
*/
public static void insertImg(WritableSheet dataSheet, int col, int row, int width,
int height, File imgFile){
WritableImage img = new WritableImage(col, row, width, height, imgFile);
dataSheet.addImage(img);
}
, , :
try {
//
WritableWorkbook workbook = Workbook.createWorkbook(new File("D:/test1.xls"));
//
WritableSheet imgSheet = workbook.createSheet("Images",0);
//
File imgFile = new File("D:/1.png");
// ,
insertImg(imgSheet,0,1,6,6,imgFile);
workbook.write();
workbook.close();
} catch (IOException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
jxl png ,jpg gif
.
, , , ,
/** Excel
* @param dataSheet
* @param left
* @param center
* @param right
*/
public static void setHeader(WritableSheet dataSheet,String left,String center,String right){
HeaderFooter hf = new HeaderFooter();
hf.getLeft().append(left);
hf.getCentre().append(center);
hf.getRight().append(right);
//
dataSheet.getSettings().setHeader(hf);
//
//dataSheet.getSettings().setFooter(hf);
}
:
try {
//
WritableWorkbook workbook = Workbook.createWorkbook(new File("D:/test1.xls"));
//
WritableSheet dataSheet = workbook.createSheet(" ",0);
ExcelUtils.setHeader(dataSheet, "chb", "2007-03-06", " 1 , 3 ");
workbook.write();
workbook.close();
} catch (IOException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
、 sql2Excel
, , linux , ssh , mysql , 。 , , windows, putty EMS , ft!
, , Excel , , , , 。
jdbc , , 。
Connection con;
Statement state;
/**
* @param serverIp
* @param dataBase
* @param userName
* @param password
* @throws ClassNotFoundException
* @throws SQLException
*/
public void init(String serverIp,String dataBase,String userName,String password) throws ClassNotFoundException, SQLException{
Class.forName("com.mysql.jdbc.Driver");
//
String url="jdbc:mysql://"+serverIp+"/"+dataBase+"?useUnicode=true&characterEncoding=GB2312";
con=DriverManager.getConnection(url,userName,password);
}
/**
* @param sql
* @return
* @throws SQLException
*/
public ResultSet getResultSet(String sql) throws SQLException{
state = con.createStatement();
ResultSet res = state.executeQuery(sql);
return res;
}
/**
* @throws SQLException
*/
public void close() throws SQLException{
if(con!=null)
con.close();
if(state!=null)
state.close();
}
ResultSet Excel
Excel, jxl, : java Excel
/** Excel
* @param rs
* @param file
* @throws SQLException
*/
public void writeExcel(ResultSet rs,File file) throws SQLException{
WritableWorkbook wwb = null;
try{
// Workbook (Workbook)
wwb = Workbook.createWorkbook(file);
} catch (IOException e){
e.printStackTrace();
}
if(wwb!=null){
WritableSheet ws = wwb.createSheet("sheet1", 0);
int i=0;
while(rs.next()){
Label label1 = new Label(0, i, rs.getString("id"));
Label label2 = new Label(1, i, rs.getString("category"));
try {
ws.addCell(label1);
ws.addCell(label2);
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
i++;
}
try {
//
wwb.write();
// ,
wwb.close();
} catch (IOException e) {
e.printStackTrace();
} catch (WriteException e){
e.printStackTrace();
}
}
}
:
Sql2Excel se = new Sql2Excel();
try {
se.init("127.0.0.1","mydabase", "root", "1234");
ResultSet rs = se.getResultSet("select id,category from xx ");
se.writeExcel(rs, new File("/root/sql2excel.xls"));
se.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}