CSDN博客

img fengyuanfa

http://www.andykhan.com/

发表于2008/9/30 13:59:00  515人阅读

http://www.andykhan.com/

 

JExcelApi is a Java API for reading, writing and modifying the contents of Excel spreadsheets
Orinoco is a Java API for generating text PDF documents

 

 

package excel;

import java.io.*;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import jxl.*;
import jxl.write.*;

@SuppressWarnings("unused")
public class ExcelDemo {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  String sourcefile = "test.xls";
  
  ReadFromExcel(sourcefile);
  
  WriteToExcel(sourcefile);
 }
 
 private static void ReadFromExcel(String fileName)
 {
  try
  {
   File file = new File(fileName);
   if(!file.exists())
   {
    return;
   }
   InputStream is = new FileInputStream(fileName);
   
   jxl.Workbook rwb = Workbook.getWorkbook(is);
   
   Sheet rs = rwb.getSheet(0);
   System.out.println("行数: " + rs.getRows());
   System.out.println("列数: " + rs.getColumns());
   
   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   
   Date startDate = new Date();   
   System.out.println("ReadFromExcel開始時間: " + sdf.format(startDate));
   String strCell  = "Cell({0}, {1})";
   for(int col = 0;col < rs.getColumns();col++)
   {
    for(int row = 0;row < rs.getRows();row++)
    {
     Cell c00 = rs.getCell(col, row);
     String strc00 = c00.getContents();
     
     System.out.println(MessageFormat.format(strCell,new Object[]{col,row}) + " value : " + strc00 + "; type : " + c00.getType());
    }
    
   }
   Date endDate = new Date();
   System.out.println("ReadFromExcel終了時間: " + sdf.format(endDate));

   rwb.close();
   
   is.close();
  }
  catch(Exception e)
  {
   e.printStackTrace();
  }
 }
 private static void WriteToExcel(String fileName)
 {
  try
  {
   File file = new File(fileName);
   if(file.exists())
   {
    file.delete();
   }
   
   jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(new File(fileName));
   
   //OutputStream os = new FileOutputStream(fileName);
   //jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(os);
   
   jxl.write.WritableSheet ws = wwb.createSheet("Test Sheet 1", 0);

   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   
   Date startDate = new Date();   
   System.out.println("WriteToExcel開始時間: " + sdf.format(startDate));
   //jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#.##");
   //jxl.write.WritableCellFormat wcfN = new jxl.write.WritableCellFormat(nf);
   for(int col = 0;col < 50;col++)
   {
    for(int rowj = 0;rowj < 1000;rowj++)
    {
     //jxl.write.Number labelNF = new jxl.write.Number(i, j, 3.1415926 * i * j, wcfN);
     jxl.write.Number labelNF = new jxl.write.Number(col, rowj, 3.1415926 * col * rowj);
     ws.addCell(labelNF);
    }
   }
   Date endDate = new Date();
   System.out.println("WriteToExcel終了時間: " + sdf.format(endDate));
   
   wwb.write();
   wwb.close();
  }
  catch(Exception e)
  {
   e.printStackTrace();
  }
 }
}

0 0

相关博文

我的热门文章

img
取 消
img