excel

本文介绍如何使用Java和Apache POI库来读取、写入Excel文件(.xls格式),并提供了具体的代码示例,包括如何创建单元格、设置单元格样式及边框。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需要apache的poi包

 

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class test {

 public static void main(String[] args) {

  try {    
      FileInputStream file = new FileInputStream("c:/hpsg.xls");

   POIFSFileSystem fs = new POIFSFileSystem(file);

   HSSFWorkbook wb = new HSSFWorkbook(fs);

   HSSFSheet sheet1 = wb.getSheet("Sheet1");
     
      HSSFRow row = sheet1.createRow(6);
      HSSFCell cell = row.createCell((short)1);
    
      HSSFRichTextString hrts = new HSSFRichTextString("hello");
     
      cell.setCellValue(hrts);
     
      HSSFCellStyle hSSFCellStyle = wb.createCellStyle();
     
      // 给excel单元格加边框
      hSSFCellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
      hSSFCellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
      hSSFCellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
      hSSFCellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
     
      cell.setCellStyle(hSSFCellStyle);
     
      FileOutputStream fileOut = new FileOutputStream("c:/hpsg.xls");
     
      wb.write(fileOut);
     
      fileOut.close();
     
      // 本地打开文件的命令
      Runtime.getRuntime().exec("cmd /c start c:/hpsg.xls");
     
  } catch (FileNotFoundException e) {

   e.printStackTrace();
  } catch (IOException e) {

   e.printStackTrace();
  }  
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值