Java程序操作Excel

来源:互联网 发布:有手机设计软件 编辑:程序博客网 时间:2024/05/29 09:15

1、使用jxl写入03版本的Excel

    需要的包:


 写入操作:

    

public class ToExcel {

public void toExcel(ArrayList<String[]> result) {


String targetfile = "d:/out.xls";// 输出的excel文件名

String worksheet = "List";// 输出的excel文件工作表名

String[] title = { "权重", "热点词与其他词", "文档信息", "同现句子" };// excel工作表的标题


WritableWorkbook workbook;

try {

// 创建可写入的Excel工作薄,运行生成的文件在tomcat/bin下

// workbook = Workbook.createWorkbook(new File("output.xls"));

System.out.println("出入Excel开始");


OutputStream os = new FileOutputStream(targetfile);

workbook = Workbook.createWorkbook(os);


WritableSheet sheet = workbook.createSheet(worksheet, 0); // 添加第一个工作表


jxl.write.Label label;

int row = 0 ;

for (int i = 0; i < title.length; i++) {

// Label(列号,行号 ,内容 )

label = new jxl.write.Label(i, row, title[i]); // put the title in

// row1

sheet.addCell(label);

}

// 遍历ArrayList存入

for(String[] res : result){

row++;

for (int i = 0; i < title.length; i++) {

// Label(列号,行号 ,内容 )

label = new jxl.write.Label(i, row, res[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.添加Date()Time对象

// // jxl.write.Date()Time labelDT = new jxl.write.Date()Time(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("向Excel存取结束");

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]);

}


}

}


2、poi操作Excle07

所需的包:


简单操作:

public class TestWriteExcel07 {

private static Map<String, String> authorMap;

private static String toFilePath = "d:/测试.xlsx";


public static void writeExcel07() {

XSSFWorkbook wb = new XSSFWorkbook();


XSSFSheet sheet = wb.createSheet("结果"); // 工作表名

// 格式

XSSFCellStyle setBorder = wb.createCellStyle();

setBorder.setFillForegroundColor((short) 13);// 设置背景色

setBorder.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);

setBorder.setBorderBottom(XSSFCellStyle.BORDER_THIN); //下边框

setBorder.setBorderLeft(XSSFCellStyle.BORDER_THIN);//左边框

setBorder.setBorderTop(XSSFCellStyle.BORDER_THIN);//上边框

setBorder.setBorderRight(XSSFCellStyle.BORDER_THIN);//右边框

XSSFRow row = sheet.createRow(0);  // 创建第几行(从0开始)

// 第一行写上作者

String author;

int i = 1;

XSSFRichTextString xssfValue;

List<String> authorList = new LinkedList<String>();

for (Entry<String, String> entry : authorMap.entrySet()) {

author = entry.getKey();

XSSFCell cell = row.createCell(i); // 该行中的第几列(从0开始)

xssfValue = new XSSFRichTextString(author);

cell.setCellValue(xssfValue);

cell.setCellStyle(setBorder);

authorList.add(author);

System.out.println(i + ":" + author);

i++;

}

// 此程序只是一个示例,因而只写了一行

FileOutputStream outputStream = null;

try {

outputStream = new

// toFilePath为待写的文件的路径

FileOutputStream(toFilePath);

wb.write(outputStream);

outputStream.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

public static void main(String[] args) {

authorMap = new HashMap<String, String>();

authorMap.put("A1", "ABC");

authorMap.put("A2", "ACB");

writeExcel07();

}

}

其它参考:

读取Excel文件,现在比较流行的第三方jar包有apache的poi和另一个jar包jxl

1)、先见poi,这个是目前做的最突出的一个操作Excel文件的工具包,支持Excel03、Excel07版,目前最高的版本是3.8,需要下载的工具包有:poi-3.8-20120326.jar,poi-ooxml-3.8-20120323.jar,

poi-ooxml-schemas-3.8-20120326.jar,xbean.jar,dom4j.jar

如果单纯操作Excel03的话,那可以只下载poi-3.8-20120326.jar,后面几个jar包是为Excel07服务的,这是由于Excel07的文件存储结构导致的

2)、  接来下将以下jxl的操作,需要下载jxl.jar包,不过目前好像已经没有团队在维护该jar包了,而且该工具包不支持excel07的操作


网址:http://blog.sina.com.cn/s/blog_707a9f0601017a2n.html

设置格式:http://www.cnblogs.com/zhenmingliu/archive/2012/04/25/2469396.html


注意,要作以下判断,否则会创建新行,覆盖掉原内容:

XSSFRow xssfRow = sheet.getRow(rowIndex);

if (null == xssfRow) {

xssfRow = sheet.createRow(rowIndex);

}


poi读写Excle参考:http://sun.hongxu.blog.163.com/blog/static/88276192011514101931968/



0 0