jxl导入Excel

package net.ewide.util;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

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

/**
* 绘制简单的excel
* 使用JXL将数据写入excel
* @author hql
*
*/
public class JxlWrite {
private static final int headerSize=24; //头字体大小
private static final int titelSize =14; //列名字体大小
private static final int cellSize =14; //单元格字体大小
/**
* 写入excel
* @param filename  文件路径
* @param titel     标题
* @param cs 列标题样式,数组中包含列名、宽度、对齐方式
* @param list      数据
*/
public void writeExcel(File file,String titelname,List<String[]> cs,List<Object[]> list){
try {
WritableWorkbook wwb = Workbook.createWorkbook(file);
WritableSheet    ws  = wwb.createSheet(titelname, 0);
if(cs.size()>1)
ws.mergeCells(0, 0, cs.size()-1, 1);              //合并单元格 从0列,0行到传入列数,2行
Label header = new Label(0,0,titelname,getHeader()); //0列,0行,表头,样式
ws.addCell(header); //添加标题
Label l = null;
for(int i=0;i<cs.size();i++){ //循环显示列名
l = new Label(i,2,cs.get(i)[0],getTitel()); //第3行显示列名
ws.addCell(l);
ws.setColumnView(i, Integer.parseInt(cs.get(i)[1])); //设置列宽

}
ws.setRowView(0,400); //设置前3行行高
ws.setRowView(1,400);
ws.setRowView(2,500);
int begrow = 3; //从第4行显示内容
for(int j=0;j<list.size();j++){ //循环显示行
for(int a=0;a<cs.size();a++){ //循环显示列
l = new Label(a,begrow,list.get(j)[a].toString(),getCell(cs.get(a)[2]));
ws.addCell(l);
}
ws.setRowView(begrow, 400); //设置行高
begrow++;
}

wwb.write(); //输出流
wwb.close(); //关闭流

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

/**
* 设置标题样式
* @return WritableCellFormat
*/
private static WritableCellFormat getHeader(){
try {
WritableFont font = new WritableFont(WritableFont.TIMES, headerSize ,WritableFont.BOLD); //字体类型、大小、粗体
font.setColour(Colour.BLUE);                                                    //蓝色字体
WritableCellFormat format = new WritableCellFormat(font); //定义样式
format.setAlignment(jxl.format.Alignment.CENTRE);                                    //左右居中
    format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);                  //上下居中
    format.setBorder(Border.ALL,BorderLineStyle.THIN,Colour.BLACK);                     //黑色边框
    format.setBackground(Colour.YELLOW);                                                //黄色背景
    return format;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

/**
* 设置列名样式
* @return WritableCellFormat
*/
private static WritableCellFormat getTitel(){
try {
WritableFont font = new  WritableFont(WritableFont.TIMES, titelSize);
font.setColour(Colour.BLUE);
WritableCellFormat format = new  WritableCellFormat(font);
format.setAlignment(jxl.format.Alignment.CENTRE);
format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
format.setBorder(Border.ALL,BorderLineStyle.THIN,Colour.BLACK);
return format;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

/**
* 设置单元格样式
* @param width
* @return WritableCellFormat
*/
private static WritableCellFormat getCell(String aln){
try {
WritableFont font = new  WritableFont(WritableFont.TIMES, cellSize);
WritableCellFormat format = new  WritableCellFormat(font);
if(aln!=null){
if(aln.equals("right")){
format.setAlignment(jxl.format.Alignment.RIGHT);
}else if(aln.equals("left")){
format.setAlignment(jxl.format.Alignment.LEFT);
}else if(aln.equals("center")){
format.setAlignment(jxl.format.Alignment.CENTRE);
}
}
format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
format.setBorder(Border.ALL,BorderLineStyle.THIN,Colour.BLACK);
return format;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

/**
* @param args
*/
public static void main(String[] args) {
File file = new File("D:\\区域管理.xls");
String name ="区域管理";
List<String[]> cs = new ArrayList<String[]>();
String[] a = {"资料号","15","left"};
String[] b = {"姓名","15","center"};
String[] c = {"地址","30","left"};
String[] d = {"电话","15","right"};
cs.add(a);
cs.add(b);
cs.add(c);
cs.add(d);
List<Object[]> list = new ArrayList<Object[]>();
String[] e = {"100283946","张大","崇仁路","13124563563"};
String[] f = {"100283947","张二","","13728372846"}; //传入null值不能显示
String[] g = {"100283948","张么","110","027-110"};
list.add(e);
list.add(f);
list.add(g);

JxlWrite jw = new JxlWrite();
jw.writeExcel(file, name, cs, list);
System.out.println("导出成功");

}

}

你可能感兴趣的:(C++,c,C#,F#,Excel)