Java向Excel文件写入数据

news/2024/7/21 5:38:50 标签: java, excel

1、依赖包配置

 <dependency>
   <groupId>net.sourceforge.jexcelapi</groupId>
   <artifactId>jxl</artifactId>
   <version>2.6.12</version>

 </dependency>

2、java实现类

java">package com.bjsd.apps.bidding.service.impl;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

public class ExcelOperaation {
  public void writeExcel(String path, List<String[]> list, String sheet, String[] title) {
    try {
      // 创建Excel工作薄
      WritableWorkbook wwb = null;
      // 新建立一个jxl文件
      OutputStream os = new FileOutputStream(path);
      wwb = Workbook.createWorkbook(os);
      // 添加第一个工作表并设置第一个Sheet的名字
      WritableSheet sheets = wwb.createSheet(sheet, 1);
      Label label;
      for (int i = 0; i < title.length; i++) {
        // Label(x,y,z) 代表单元格的第x+1列,第y+1行, 内容z
        // 在Label对象的子对象中指明单元格的位置和内容
        // label = new Label(i, 0, title[i]);
        label = new Label(i, 0, title[i], getHeader());
        // 设置列宽
        sheets.setColumnView(i, 20);
        // sheets.setColumnView(4, 100);
        // 将定义好的单元格添加到工作表中
        sheets.addCell(label);
      }

      // 设置单元格属性
      WritableCellFormat wc = new WritableCellFormat();
      // 设置居中
      wc.setAlignment(Alignment.CENTRE);
      // 设置边框线
      wc.setBorder(Border.ALL, BorderLineStyle.THIN);

      for (int i = 0; i < list.size(); i++) {
        String[] arrData = list.get(i);
        for (int j = 0; j < arrData.length; j++) {
          // 向特定单元格写入数据
          // sheets.setColumnView(j, 20);
          label = new Label(j, 1 + i, arrData[j], wc);
          sheets.addCell(label);
        }
      }
      // 写入数据
      wwb.write();
      // 关闭文件
      wwb.close();
    }
    catch (Exception e) {
      e.printStackTrace();
      return;
    }
  }

  public static WritableCellFormat getHeader() {
    // 定义字体
    WritableFont font = new WritableFont(WritableFont.TIMES, 10, WritableFont.BOLD);
    try {
      // 黑色字体
      font.setColour(jxl.format.Colour.BLACK);
    }
    catch (WriteException e1) {
      e1.printStackTrace();
    }
    WritableCellFormat format = new WritableCellFormat(font);
    try {
      // 左右居中
      format.setAlignment(jxl.format.Alignment.CENTRE);
      // 上下居中
      format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
      // 黑色边框
      format.setBorder(Border.ALL, BorderLineStyle.THIN, jxl.format.Colour.BLACK);
      // 黄色背景
      format.setBackground(jxl.format.Colour.YELLOW);
    }
    catch (WriteException e) {
      e.printStackTrace();
    }
    return format;
  }

  public static void main(String args[]) {
    ExcelOperaation excel = new ExcelOperaation();
    // 文件保存路径
    String path = "E:/test.xlsx";
    // 准备测试数据
    List<String[]> list = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
      String content[] = { i + "", "hello" };
      list.add(content);
    }

    // sheet页名称
    String sheet = "hhh";
    // 列名
    String title[] = { "id", "content" };
    excel.writeExcel(path, list, sheet, title);
  }
}



http://www.niftyadmin.cn/n/1514049.html

相关文章

android录音波浪动画_Android 自定义波浪动画--让进度浪起来~

waveview《Android 自定义波浪动画之"让进度浪起来~"》转载请注明来自 傻小孩b_移动开发(http://www.jianshu.com/users/d388bcf9c4d3)喜欢的可以关注我&#xff0c;不定期总结文章&#xff01;您的支持是我的动力哈&#xff01;前言首先贝塞尔曲线原理我在这里就不多…

用labview设计jk触发器_基于LabVIEW的基本触发器设计.doc

您所在位置&#xff1a;网站首页 > 海量文档&nbsp>&nbsp计算机&nbsp>&nbspLabview基于LabVIEW的基本触发器设计.doc15页本文档一共被下载&#xff1a;次,您可全文免费在线阅读后下载本文档。下载提示1.本站不保证该用户上传的文档完整性&#xff0c…

ThreadPoolExecutor运转机制及BlockingQueue详解

文章来源&#xff1a;http://blog.csdn.net/zmx729618/article/details/51453792 1、ThreadPoolExecutor的构建参数 最近发现几起对ThreadPoolExecutor的误用&#xff0c;其中包括自己&#xff0c;发现都是因为没有仔细看注释和内部运转机制&#xff0c;想当然的揣测参数导致&…

vue怎么改logo_修改vue项目的title左边的logo

1、icon命名为favicon.ico放在项目根目录&#xff1b;2、在index.html中写入&#xff1a;3、修改build下面webpack.prod.conf.js和webpack.dev.conf.js&#xff1a;修改&#xff1a;添加一行favicon: path.resolve(favicon.ico), // 引入图片地址webpack.prod.conf.js&#xff…

generatorConfiguration配置文件及其详细解读

转自&#xff1a;http://blog.csdn.net/weifen234/article/details/49638023# [html] view plaincopy<?xml version"1.0" encoding"UTF-8" ?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configur…

python字典统计男女比例_python统计男女比例-女性时尚流行美容健康娱乐mv-ida网...

女性时尚流行美容健康娱乐mv-ida网 mvida时尚娱乐网 首页 美容 护肤 化妆技巧 发型 服饰 健康 情感 美体 美食 娱乐 明星八卦 首页 > 高级搜索 python 实现代码行数 统计 工具 我们经常想要 统计 项目的代码行数&#xff0c;但是如果想 统计 功能比较完善可能就不是那么简单…

HttpClient获取重定向之后的网址信息

转自&#xff1a;http://blog.csdn.net/jadyer/article/details/8656019 [java] view plain copy print?package com.jadyer.httpclient; import org.apache.http.HttpEntity; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache…

C++、Java、JavaScript中的正则表达式

转自&#xff1a;http://blog.csdn.net/luoweifu/article/details/42613533 编程思想之正则表达式 什么是正则表达式&#xff1f; 正则表达式(Regular Expression)就是用某种模式去匹配一类字符串的公式。如你要在一篇文章中查找第一个字是“罗”最后一个字是“浩”的三个字的…