JAVA操作excel的一些例子

來源:互聯網
上載者:User

   JAVA操作EXCEL有兩種辦法,一個用POI,一個用JXI,好象聽說JXI對中文的支援會稍微好些.

1 用POI,一個寫的例子:
  

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.poifs.filesystem.*;
import java.io.*;

public class Write {

 /**
  * @param args
  */
 public static void main(String[] args) {
  try
  {
  // TODO Auto-generated method stub
HSSFWorkbook wb=new HSSFWorkbook();
   HSSFSheet sheet=wb.createSheet("new sheet");
   HSSFRow row=sheet.createRow((short)1);  
   row.createCell((short)1).setCellValue(1.2);  
   row.createCell((short)2).setCellValue("hellow way"); 
   row.createCell((short)3).setCellValue("aaaaa");    
   FileOutputStream fileout=new FileOutputStream("c:\\aa.xls"); 
      wb.write(fileout);                         
      fileout.close();
     
      System.out.println("aaa");
  }
  catch(Exception e)
  {}
 }

2 讀的例子:
   CustomerDAO customerDAO = new CustomerDAO();
   String accounts = (String) request.getSession()
     .getValue("accounts");

   String fileToBeRead = request.getRealPath("/")
     + "customer\\excel\\upload_" + accounts + ".xls";
   try {
    HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(
      fileToBeRead));
    HSSFSheet sheet = workbook.getSheetAt(0);
    /* 讀取檔案中的資訊,並把資料存入資料庫 */
    for (int j = 1;; j++) {
     HSSFRow row = sheet.getRow(j);
     if (row != null) {
      CustomerDTO customerDTO = new CustomerDTO();
      HSSFCell cell;
      int i = 0;
      /* 跳過第一列的customerInformationID */
      i++;
      cell = row.getCell((short) i++);
      customerDTO.setCompanyName(cell.getStringCellValue());
      cell = row.getCell((short) i++);
      customerDTO.setCustomerName(cell.getStringCellValue());
      cell = row.getCell((short) i++);
      customerDTO.setNameForShort(cell.getStringCellValue());
      cell = row.getCell((short) i++);
      customerDTO
        .setCompanyAddress(cell.getStringCellValue());
      cell = row.getCell((short) i++);
      customerDTO.setCountry(cell.getStringCellValue());
      cell = row.getCell((short) i++);
      customerDTO.setProvince(cell.getStringCellValue());
      cell = row.getCell((short) i++);
      customerDTO.setCity(cell.getStringCellValue());
      cell = row.getCell((short) i++);
      customerDTO.setPostalcode(cell.getStringCellValue());
      cell = row.getCell((short) i++);
      customerDTO.setCompanyHomePage(cell
        .getStringCellValue());
      
      customerDAO.insertCustomer(customerDTO);
     } else {
      break;
     }
    }
   } catch (Exception e) {
    e.printStackTrace();
   }

  而JXI的話,可以看這兩篇文介紹
  http://malipei.javaeye.com/blog/70044

http://www.alchemistrex.com/article.asp?id=138

http://www.blogjava.net/jhengfei/archive/2006/05/22/47480.html

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.