http://www.andykhan.com/

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值