依赖

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

  

XLS

package export;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List; import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook; public class XLSUtil implements Serializable { public static boolean createXls(String tableName,List<Object> head, List<List<Object>> dataList,
String outPutPath, String filename){
WritableWorkbook book = null;
try {
String filePath = outPutPath+filename+".xls";
XLSUtil.checkFile(filePath);
// 打开文件
book = Workbook.createWorkbook(new File(filePath));
WritableSheet sheet = book.createSheet(filename, );
if(head!=null&&head.size()>){
for (int i = ; i < head.size(); i++) {
Label label = new Label(i, , (String) head.get(i));
// 将定义好的单元格添加到工作表中
sheet.addCell(label);
}
}
if(dataList!=null&&dataList.size()>){
for (int i = ; i < dataList.size(); i++) {
List<Object> list = dataList.get(i);
// 保存数字的单元格必须使用Number的完整包路径
for (int j = ; j < list.size(); j++) {
Label label = new Label(j, i+, (String) list.get(j));
sheet.addCell(label);
}
}
}
// 写入数据并关闭文件
book.write();
} catch (Exception e) {
System.out.println(e);
}finally{
if(book!=null){
try {
book.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return true;
} public static void checkFile(String filename) {
File file = new File(filename);
if(!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
} } public static void main(String[] args) { String tableName = "学生";
List<Object> head = new ArrayList<Object>();
head.add("你好");
head.add("我好");
head.add("大家好"); List<List<Object>> dataList = new ArrayList<List<Object>>();
List<Object> head1 = new ArrayList<Object>();
head1.add("");
head1.add("");
head1.add(""); dataList.add(head1);
dataList.add(head1);
dataList.add(head1); String filename = "我的文件";
String outPutPath = "F:/test/aqa/"; XLSUtil.createXls(tableName, head, dataList, outPutPath, filename);
// XLSUtil.xlsExport(tableName , head , dataList, outPutPath , filename );
}
}

csv

package export;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List; public class CSVUtil implements Serializable {
/**
* CSV文件生成方法
* @param head
* @param dataList
* @param outPutPath
* @param filename
* @return
*/
public static File createCSVFile(List<Object> head, List<List<Object>> dataList,
String outPutPath, String filename) {
File csvFile = null;
BufferedWriter csvWtriter = null;
try {
csvFile = new File(outPutPath + File.separator + filename + ".csv");
System.out.println(File.separator);
File parent = csvFile.getParentFile();
if (parent != null && !parent.exists()) {
parent.mkdirs();
}
csvFile.createNewFile();
csvWtriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream( csvFile)));
// 写入文件头部
writeRow(head, csvWtriter);
// 写入文件内容
for (List<Object> row : dataList) {
writeRow(row, csvWtriter);
}
csvWtriter.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
csvWtriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return csvFile;
} /**
* 写一行数据方法
* @param row
* @param csvWriter
* @throws IOException
*/
private static void writeRow(List<Object> row, BufferedWriter csvWriter) throws IOException {
// 写入文件头部
for (Object data : row) {
StringBuffer sb = new StringBuffer();
String rowStr = sb.append("\"").append(data).append("\",").toString();
csvWriter.write(rowStr);
}
csvWriter.newLine();
} public static void main(String[] args) {
List<Object> exportData = new ArrayList<Object>();
exportData.add("第一列");
exportData.add("第二列");
exportData.add("第三列");
List<List<Object>> datalist = new ArrayList<List<Object>>();
List<Object> data=new ArrayList<Object>();
data.add("111");
data.add("222");
data.add("333");
List<Object> data1=new ArrayList<Object>();
data1.add("444");
data1.add("555");
data1.add("666");
datalist.add(data);
datalist.add(data1);
String path = "d:/export/";
String fileName = "文件导出"; File file = CSVUtil.createCSVFile(exportData, datalist, path, fileName);
String fileName2 = file.getName();
System.out.println("文件名称:" + fileName2);
}
}

excel导入导出的两种方式:csv和XLS的更多相关文章

  1. Spark:DataFrame批量导入Hbase的两种方式(HFile、Hive)

    Spark处理后的结果数据resultDataFrame可以有多种存储介质,比较常见是存储为文件.关系型数据库,非关系行数据库. 各种方式有各自的特点,对于海量数据而言,如果想要达到实时查询的目的,使 ...

  2. Hive数据导入导出的几种方式

    一,Hive数据导入的几种方式 首先列出讲述下面几种导入方式的数据和hive表. 导入: 本地文件导入到Hive表: Hive表导入到Hive表; HDFS文件导入到Hive表; 创建表的过程中从其他 ...

  3. Hive数据导入导出的n种方式

    Tutorial-LoadingData Hive加载数据的6种方式 #格式 load data [local] inpath '/op/datas/xxx.txt' [overwrite] into ...

  4. oracle数据的导入导出(两种方法三种方式)

    大概了解数据库中数据的导入导出.在oracle中,导入导出数据的方法有两种,一种是使用cmd命令行的形式导入导出数据,另一种是使用PL/SQL工具导入导出数据. 1,使用cmd命令行导入导出数据 1. ...

  5. Python3 动态导入模块的两种方式

    动态导入模块就是只知道str类型的模块名字符串,通过这个字符串导入模块 需要导入的模块: #!/usr/bin/env python # _*_ coding:utf-8 _*_ # Author:C ...

  6. python导入模块的两种方式

    第一种 from support import * 这种方式导入后可以直接调用(有命名冲突问题)命名冲突后定义的覆盖前定义的 如果在函数导入前定义 则导入函数覆盖 否则相反 if __name__ = ...

  7. C++ DLL导出的两种方式和链接的两种方式

    第一种 导出方式 extern "C" _declspec(dllexport) int Plus(int x, int y); extern "C" _dec ...

  8. android studio导入jar的两种方式

    一.第一种是打开工程所在Project Structure,然后选择Dependencies,点击那个加号选择File Dependency ,然后再Libs文件夹中选择要导入的jar包 1. 2. ...

  9. Excel文件读取的两种方式

    1.Pandas库的读取操作 from pandas import read_excel dr=read_excel(filename,header) dr#dataframe数据 dw=DataFr ...

随机推荐

  1. FB工作流相关

    1.初始化项目 gitlab上建立一个仓库 在命令行中运行git clone,将仓库克隆到本地 在命令行中找到前端模版文件(模版文件夹(front-template)有个sao.js脚本,用来在本地按 ...

  2. Linux下的文件操作——基于文件描述符的文件操作(2)

    文件描述符的复制 MMAP文件映射 ftruncate修改文件大小 文件描述符的复制 ​ 系统调用函数dup和dup2可以实现文件描述符的复制,经常用来重定向进程的stdin(0), stdout(1 ...

  3. c#属性 ——面向对象

    String. Format(字符串格式化输出) 相当于Console.WriteLine(字符串格式化输出); 而String.Format是返回一个字符串 属性: 因为把字段全public,会非常 ...

  4. postgresql定位分析消耗CPU高的SQL语句

    第一步:使用TOP命令查看占用CPU高的postgresql进程,并获取该进程的ID号,如图该id号为3640 第二步:切换到postgres用户,并且psql连接到数据库,执行如下查询语句 SELE ...

  5. Redis-Migrate-Tool 使用详解

    注意:目前不支持4.0.X及以上的redis使用 Redis 集群迁移工具,基于redis复制,快速,稳定. github链接:https://github.com/vipshop/redis-mig ...

  6. tf.nn.nce_loss

    def nce_loss(weights,biases,inputs,labels,num_sampled,num_classes,num_true=1,sampled_values=None,rem ...

  7. webpack、npm、nginx常用命令

    webpack命令:webpack --watch 监听变动并自动打包,简写-wwebpack -p --progress --color 压缩混淆脚本webpack -d  生成映射文件,告知那些模 ...

  8. Elasticsearch增删改查

    面向文档 document数据格式 应用系统的数据结构都是面向对象的,复杂的 对象数据存储到数据库中,只能拆解开来,变为扁平的多张表,每次查询的时候还得还原回对象格式,相当麻烦 ES是面向文档的,文档 ...

  9. Android悬浮框,在Service中打开悬浮窗;在Service中打开Dialog;

    文章介绍了如何在Service中显示悬浮框,在Service中弹出Dialog,在Service中做耗时的轮询操作: 背景需求: 公司的项目现在的逻辑是这样的:发送一个指令,然后3秒一次轮询去查询这个 ...

  10. requests模块报错:Use body.encode('utf-8') if you want to send it encoded in UTF-8.

    在做 企业向微信用户个人付款  功能时,调用第三方sdk,在 进行 requests 的post请求时, 代码如下 req = requests.post(url, data=data,cert(ap ...