java excel WriteExcelAndCSVUtils
来源:互联网 发布:10月外贸数据 编辑:程序博客网 时间:2024/05/29 16:50
/**
* WriteExcetlAndCSVUtils
*/
package com.airintelli.ssm.utils;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;
import com.opencsv.CSVWriter;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
/**
* 根据Lis集合存储的数据写入到excel或csv文件中
*
* @author xp
*
*/
public class WriteExcetlAndCSVUtils {
/**
* 导出文件
*
* @param fileType
* 导出是csv文件
* @param fileName
* 导出文件全路径名
* @param list
* 导出的数据集合数组
* @param tableName
* 导出为excel时,给工作溥的名称
* @return
* @throws Exception
*/
public static void exportDataFile(String fileType, String fileName, List<String[]> list, String tableName)
throws Exception {
if (fileType != null && fileType.length() > 0) {
if (list != null && list.size() > 0) {
if (fileType.equalsIgnoreCase("xls")) {
writeDataToExcel(fileName, list, tableName);
} else if (fileType.equalsIgnoreCase("csv")) {
writeCSV(fileName, list);
}
} else {
throw new Exception("导出的数据内容不能为空 ");
}
} else {
throw new Exception("请确认您想要导出的文件格式");
}
}
/**
* 把数据库中获取到的数据写入到指定的csv文件中
*
* @param fileName
* @param list
* @throws Exception
*/
public static void writeCSV(String fileName, List<String[]> list) throws Exception {
File file = new File(fileName);
OutputStreamWriter outputStreamWriter = null;
BufferedWriter bufferedWriter = null;
CSVWriter csvWriter = null;
try {
outputStreamWriter = new OutputStreamWriter(new FileOutputStream(file), "utf-8");
bufferedWriter = new BufferedWriter(outputStreamWriter);
csvWriter = new CSVWriter(bufferedWriter, ',');
if (list != null && list.size() > 0) {
csvWriter.writeAll(list, true);
}
} catch (UnsupportedEncodingException | FileNotFoundException e) {
throw new Exception(e.getMessage());
} finally {
try {
if (csvWriter != null) {
csvWriter.flush();
csvWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 把数据库中获取到的数据写入到指定的excel文件中
*
* @param fileName
* 文件写入的路径和名称
* @param list
* 从数据库读取的数据转换为《数组集合》
* @param tableName
* 写入excel文件中工作溥的名称
* @throws Exception
*/
public static void writeDataToExcel(String fileName, List<String[]> list, String tableName) throws Exception {
if (list.size() > 0) {
File file = new File(fileName);
// 创建excel表格
WritableWorkbook workbook = null;
try {
workbook = Workbook.createWorkbook(file);
// 创建sheet
WritableSheet sheet = workbook.createSheet(tableName, 1);
for (int i = 0; i < list.size(); i++) {
for (int j = 0; j < list.get(i).length; j++) {
// 创建label对象
Label label1 = new Label(j, i, list.get(i)[j]);
// 添加到sheet中
sheet.addCell(label1);
}
}
// sheet写入到excel中
workbook.write();
} catch (Exception e) {
throw new Exception(e.getMessage());
} finally {
try {
if (workbook != null) {
workbook.close();
}
} catch (WriteException | IOException e) {
throw new Exception(e.getMessage());
}
}
} else {
throw new Exception("读取的数据不能为空");
}
}
}
* WriteExcetlAndCSVUtils
*/
package com.airintelli.ssm.utils;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;
import com.opencsv.CSVWriter;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
/**
* 根据Lis集合存储的数据写入到excel或csv文件中
*
* @author xp
*
*/
public class WriteExcetlAndCSVUtils {
/**
* 导出文件
*
* @param fileType
* 导出是csv文件
* @param fileName
* 导出文件全路径名
* @param list
* 导出的数据集合数组
* @param tableName
* 导出为excel时,给工作溥的名称
* @return
* @throws Exception
*/
public static void exportDataFile(String fileType, String fileName, List<String[]> list, String tableName)
throws Exception {
if (fileType != null && fileType.length() > 0) {
if (list != null && list.size() > 0) {
if (fileType.equalsIgnoreCase("xls")) {
writeDataToExcel(fileName, list, tableName);
} else if (fileType.equalsIgnoreCase("csv")) {
writeCSV(fileName, list);
}
} else {
throw new Exception("导出的数据内容不能为空 ");
}
} else {
throw new Exception("请确认您想要导出的文件格式");
}
}
/**
* 把数据库中获取到的数据写入到指定的csv文件中
*
* @param fileName
* @param list
* @throws Exception
*/
public static void writeCSV(String fileName, List<String[]> list) throws Exception {
File file = new File(fileName);
OutputStreamWriter outputStreamWriter = null;
BufferedWriter bufferedWriter = null;
CSVWriter csvWriter = null;
try {
outputStreamWriter = new OutputStreamWriter(new FileOutputStream(file), "utf-8");
bufferedWriter = new BufferedWriter(outputStreamWriter);
csvWriter = new CSVWriter(bufferedWriter, ',');
if (list != null && list.size() > 0) {
csvWriter.writeAll(list, true);
}
} catch (UnsupportedEncodingException | FileNotFoundException e) {
throw new Exception(e.getMessage());
} finally {
try {
if (csvWriter != null) {
csvWriter.flush();
csvWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 把数据库中获取到的数据写入到指定的excel文件中
*
* @param fileName
* 文件写入的路径和名称
* @param list
* 从数据库读取的数据转换为《数组集合》
* @param tableName
* 写入excel文件中工作溥的名称
* @throws Exception
*/
public static void writeDataToExcel(String fileName, List<String[]> list, String tableName) throws Exception {
if (list.size() > 0) {
File file = new File(fileName);
// 创建excel表格
WritableWorkbook workbook = null;
try {
workbook = Workbook.createWorkbook(file);
// 创建sheet
WritableSheet sheet = workbook.createSheet(tableName, 1);
for (int i = 0; i < list.size(); i++) {
for (int j = 0; j < list.get(i).length; j++) {
// 创建label对象
Label label1 = new Label(j, i, list.get(i)[j]);
// 添加到sheet中
sheet.addCell(label1);
}
}
// sheet写入到excel中
workbook.write();
} catch (Exception e) {
throw new Exception(e.getMessage());
} finally {
try {
if (workbook != null) {
workbook.close();
}
} catch (WriteException | IOException e) {
throw new Exception(e.getMessage());
}
}
} else {
throw new Exception("读取的数据不能为空");
}
}
}
阅读全文
0 0
- java excel WriteExcelAndCSVUtils
- Java Excel 操作excel
- java excel
- JAVA EXCEL
- java excel
- java EXCEL
- java excel
- java excel
- java excel
- java excel
- Java Excel
- java excel
- Excel Java
- java-Excel
- java 解析 excel数据excel
- java 操作 Excel,java导出excel
- Java操作Excel JAVA EXCEL API
- Java Excel API Tutorial
- 设计模式
- 我
- Django项目中如何在模板文件中加载静态文件
- Spring面试题分享()
- Find The Multiple(深搜找倍数)
- java excel WriteExcelAndCSVUtils
- bzoj1225: [HNOI2001] 求正整数
- 约瑟夫环
- Cookie路径的作用范围示例代码
- VMvare学习4_此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态
- 安卓 使用Snackbar Failed to resolve: com.android.support:design:26.0.0 解决方案
- 非空验证 注册用户信息
- 信号的采样以及matlab实现
- python进程间通信-Queue