导航:首页 > 编程语言 > javalist转excel

javalist转excel

发布时间:2023-03-02 08:47:29

java中怎么把List集合存成Excel表格

首先这个List尽量是Object类型的,也就是:List<Object[]> list
其次就是开始遍历这个list
int currentRowNum = startRowNum-1;//从第几行开始写
int currentColNum = startColNum-1;//从第几列开始写
for (Object[] objects : list) {//遍历list
XSSFRow row = sheet.createRow(currentRowNum);//一个list就是一行,创建一个行
for (Object object : objects) {//遍历每一个list的Object[]数组
if (object==null) {
XSSFCell cell = row.createCell(currentColNum++, XSSFCell.CELL_TYPE_BLANK);//如果该单元格为空,则创建一个空格子,不然数据会移位
cell.setCellValue("");//将这个空格设置为空字符串
}else if(object instanceof Double || object instanceof Float){//判断这个格子放的数据的类型,其他的同理
XSSFCell cell = row.createCell(currentColNum++, XSSFCell.CELL_TYPE_NUMERIC);//如果是Double或者Float类型的,则用这个方式
cell.setCellValue(Double.parseDouble(object.toString()));
}else if(object instanceof Long || object instanceof Integer){
XSSFCell cell = row.createCell(currentColNum++, XSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue(Double.parseDouble(object.toString()));
}else if(object instanceof Date){
XSSFCell cell = row.createCell(currentColNum++, XSSFCell.CELL_TYPE_STRING);
cell.setCellValue(new SimpleDateFormat("dd-MM-yyyy").format((Date)object));
}else if(object instanceof Boolean){
XSSFCell cell = row.createCell(currentColNum++, XSSFCell.CELL_TYPE_BOOLEAN);
cell.setCellValue((Boolean)object);
}else{
XSSFCell cell = row.createCell(currentColNum++, XSSFCell.CELL_TYPE_STRING);
cell.setCellValue(String.valueOf(object));
}
}
currentRowNum++;//写完第一个list,跳到下一行
currentColNum = startColNum-1;
}

这种是poi操作EXCEL的方法哈。。
jxl操作略有不同。

② 如何用Java导出数据存到excel里面

java中jxl导出数据到excel的例子
import jxl.*;
import jxl.write.*;
import java.io.*;
import java.io.File.*;
import java.util.*;

public class excel
{
public static void main(String[] args)
{

String targetfile = "c:/out.xls";//输出的excel文件
String worksheet = "List";//输出的excel文件工作表名
String[] title = {"ID","NAME","DESCRIB"};//excel工作表的标题

WritableWorkbook workbook;
try
{
//创建可写入的工作薄,运行生成的文件在tomcat/bin下
//workbook = Workbook.createWorkbook(new File("output.xls"));
System.out.println("begin");

OutputStream os=new FileOutputStream(targetfile);
workbook=Workbook.createWorkbook(os);

WritableSheet sheet = workbook.createSheet(worksheet, 0); //添加第一个工作表
//WritableSheet sheet1 = workbook.createSheet("MySheet1", 1); //可添加第二个工作
/*
jxl.write.Label label = new jxl.write.Label(0, 2, "A label record"); //put a label in cell A3, Label(column,row)
sheet.addCell(label);
*/

jxl.write.Label label;
for (int i=0; i<title.length; i++)
{
//Label(列号,行号 ,内容 )
label = new jxl.write.Label(i, 0, title[i]); //put the title in row1
sheet.addCell(label);
}

//下列添加的对字体等的设置均调试通过,可作参考用

//添加数字
jxl.write.Number number = new jxl.write.Number(3, 4, 3.14159); //put the number 3.14159 in cell D5
sheet.addCell(number);

//添加带有字型Formatting的对象
jxl.write.WritableFont wf = new jxl.write.WritableFont(WritableFont.TIMES,10,WritableFont.BOLD,true);
jxl.write.WritableCellFormat wcfF = new jxl.write.WritableCellFormat(wf);
jxl.write.Label labelCF = new jxl.write.Label(4,4,"文本",wcfF);
sheet.addCell(labelCF);

//添加带有字体颜色,带背景颜色 Formatting的对象
jxl.write.WritableFont wfc = new jxl.write.WritableFont(WritableFont.ARIAL,10,WritableFont.BOLD,false,jxl.format.UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.RED);
jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat(wfc);
wcfFC.setBackground(jxl.format.Colour.BLUE);
jxl.write.Label labelCFC = new jxl.write.Label(1,5,"带颜色",wcfFC);
sheet.addCell(labelCFC);

//添加带有formatting的Number对象
jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#.##");
jxl.write.WritableCellFormat wcfN = new jxl.write.WritableCellFormat(nf);
jxl.write.Number labelNF = new jxl.write.Number(1,1,3.1415926,wcfN);
sheet.addCell(labelNF);

//3.添加Boolean对象
jxl.write.Boolean labelB = new jxl.write.Boolean(0,2,false);
sheet.addCell(labelB);

//4.添加DateTime对象
jxl.write.DateTime labelDT = new jxl.write.DateTime(0,3,new java.util.Date());
sheet.addCell(labelDT);

//添加带有formatting的DateFormat对象
jxl.write.DateFormat df = new jxl.write.DateFormat("ddMMyyyyhh:mm:ss");
jxl.write.WritableCellFormat wcfDF = new jxl.write.WritableCellFormat(df);
jxl.write.DateTime labelDTF = new jxl.write.DateTime(1,3,new java.util.Date(),wcfDF);
sheet.addCell(labelDTF);

//和宾单元格
//sheet.mergeCells(int col1,int row1,int col2,int row2);//左上角到右下角
sheet.mergeCells(4,5,8,10);//左上角到右下角
wfc = new jxl.write.WritableFont(WritableFont.ARIAL,40,WritableFont.BOLD,false,jxl.format.UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.GREEN);
jxl.write.WritableCellFormat wchB = new jxl.write.WritableCellFormat(wfc);
wchB.setAlignment(jxl.format.Alignment.CENTRE);
labelCFC = new jxl.write.Label(4,5,"单元合并",wchB);
sheet.addCell(labelCFC); //

//设置边框
jxl.write.WritableCellFormat wcsB = new jxl.write.WritableCellFormat();
wcsB.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THICK);
labelCFC = new jxl.write.Label(0,6,"边框设置",wcsB);
sheet.addCell(labelCFC);
workbook.write();
workbook.close();
}catch(Exception e)
{
e.printStackTrace();
}
System.out.println("end");
Runtime r=Runtime.getRuntime();
Process p=null;
//String cmd[]={"notepad","exec.java"};
String cmd[]={"C:\\Program Files\\Microsoft Office\\Office\\EXCEL.EXE","out.xls"};
try{
p=r.exec(cmd);
}
catch(Exception e){
System.out.println("error executing: "+cmd[0]);
}

}
}

③ Java如何将二维数组写入excel

java读取txt文件然后赋值二维数组实现方法如下:
package shi;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Test13 {

/**
* 读取文件
* @param filePath
* @return
*/
public static List readTxtFile(String filePath) {
List<String> list = new ArrayList<String>();
try {
String encoding = "UTF-8";
File file = new File(filePath);
if (file.isFile() && file.exists()) {
InputStreamReader read = new InputStreamReader(
new FileInputStream(file), encoding);
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
if (!lineTxt.startsWith("#"))
list.add(lineTxt);
}
read.close();
} else {
System.out.println("找不到文件");
}
} catch (Exception e) {
System.out.println("出错了");
e.printStackTrace();
}
return list;

}

/**
* 创建二维数组
* @param list
* @return
*/
public static String[][] createArray(String filePath){
List<String> list = readTxtFile(filePath);
String array[][] = new String[list.size()][];
for(int i=0;i<list.size();i++){
array[i] = new String[3];
String linetxt=list.get(i);
String[] myArray = linetxt.replaceAll("\\s+", "@").split("@");
for(int j=0;j<myArray.length;j++){
if(j<3){
array[i][j]=myArray[j];
}
}
}
return array;
}

/**
* 打印数组
* @param array
*/
public static void printArray(String array[][]){
for(int i=0;i<array.length;i++){
for(int j=0;j<array[i].length;j++){
if(j!=array[i].length-1){
System.out.print("array["+i+"]["+j+"]="+array[i][j]+",");
}
else{
System.out.print("array["+i+"]["+j+"]="+array[i][j]);
}

}
System.out.println();
}
}

public static void main(String args[]) {
String array[][] = createArray("F:\\test1.txt");
printArray(array);
}

}

④ java怎么导出excel表格

可以使用POI开源的api:
1.首先下载poi-3.6-20091214.jar,下载地址如下:
http://download.csdn.net/detail/evangel_z/3895051

2.Student.java

import java.util.Date;

public class Student
{
private int id;
private String name;
private int age;
private Date birth;

public Student()
{
}

public Student(int id, String name, int age, Date birth)
{
this.id = id;
this.name = name;
this.age = age;
this.birth = birth;
}

public int getId()
{
return id;
}

public void setId(int id)
{
this.id = id;
}

public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}

public int getAge()
{
return age;
}

public void setAge(int age)
{
this.age = age;
}

public Date getBirth()
{
return birth;
}

public void setBirth(Date birth)
{
this.birth = birth;
}

}
3.CreateSimpleExcelToDisk.java

import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class CreateSimpleExcelToDisk
{
/**
* @功能:手工构建一个简单格式的Excel
*/
private static List<Student> getStudent() throws Exception
{
List list = new ArrayList();
SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");

Student user1 = new Student(1, "张三", 16, df.parse("1997-03-12"));
Student user2 = new Student(2, "李四", 17, df.parse("1996-08-12"));
Student user3 = new Student(3, "王五", 26, df.parse("1985-11-12"));
list.add(user1);
list.add(user2);
list.add(user3);

return list;
}

public static void main(String[] args) throws Exception
{
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("学生表一");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow((int) 0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("学号");
cell.setCellStyle(style);
cell = row.createCell((short) 1);
cell.setCellValue("姓名");
cell.setCellStyle(style);
cell = row.createCell((short) 2);
cell.setCellValue("年龄");
cell.setCellStyle(style);
cell = row.createCell((short) 3);
cell.setCellValue("生日");
cell.setCellStyle(style);

// 第五步,写入实体数据 实际应用中这些数据从数据库得到,
List list = CreateSimpleExcelToDisk.getStudent();

for (int i = 0; i < list.size(); i++)
{
row = sheet.createRow((int) i + 1);
Student stu = (Student) list.get(i);
// 第四步,创建单元格,并设置值
row.createCell((short) 0).setCellValue((double) stu.getId());
row.createCell((short) 1).setCellValue(stu.getName());
row.createCell((short) 2).setCellValue((double) stu.getAge());
cell = row.createCell((short) 3);
cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(stu
.getBirth()));
}
// 第六步,将文件存到指定位置
try
{
FileOutputStream fout = new FileOutputStream("E:/students.xls");
wb.write(fout);
fout.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

阅读全文

与javalist转excel相关的资料

热点内容
金蝶kis支持win10吗 浏览:113
常州采集物联网大数据平台有哪些 浏览:950
win10休眠文件改到d盘 浏览:626
如何编程手机app软件 浏览:656
node获取文件名 浏览:367
iphoneios7怎么设置铃声 浏览:52
手机qq激活星钻 浏览:302
html中引入js文件路径 浏览:83
0基础自学编程可以看什么书 浏览:860
javapoi导出excel 浏览:212
迷你编程迷小酷为什么邮箱里没有 浏览:33
ipadqq接收的文件在哪里 浏览:15
拼多多初级看哪些数据 浏览:848
win10开机密码屏幕键盘 浏览:162
文件夹乐高 浏览:914
外置文件夹挂载 浏览:304
人人视频本地缓存文件 浏览:194
java俄罗斯方块项目描述 浏览:354
win10系统被冻结 浏览:460
excel文件批量合并 浏览:948

友情链接