ASP源码.NET源码PHP源码JSP源码JAVA源码DELPHI源码PB源码VC源码VB源码Android源码
当前位置:首页 >> 网络编程 >> Java教程 >> Java导出Excel

Java导出Excel

来源:网络整理     时间:2015-03-10     关键词:

本篇文章主要介绍了"Java导出Excel",主要涉及到方面的内容,对于Java教程感兴趣的同学可以参考一下: import java.io.File; import java.io.FileOutputStream; import java.io.IOException...

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

import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

publicclassExportExcel {publicstaticvoidmain(String[] args) throws IOException {
        // create work book
        HSSFWorkbook wb = new HSSFWorkbook();
        // create excel sheet for page 1
        HSSFSheet sheet = wb.createSheet();
        sheet.setDefaultColumnWidth(10);

        // Set Header Font
        HSSFFont headerFont = wb.createFont();
        headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        headerFont.setFontHeightInPoints((short) 12);

        // Set Header Style
        CellStyle headerStyle = wb.createCellStyle();
        headerStyle.setFont(headerFont);
        headerStyle.setAlignment(CellStyle.ALIGN_CENTER);
        headerStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);

        // Set Header Style
        CellStyle bodyStyle = wb.createCellStyle();
        bodyStyle.setAlignment(CellStyle.ALIGN_CENTER);
        bodyStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);

        int rowCount = 0;
        Row header;

        /*
         * Folllowing code parse html table
         */
        Document doc = Jsoup.connect(
                "http://www.w3school.com.cn/tags/tag_table.asp")
                .get();// Jsoup.parse(htmlData);/*
         * Display list of headers for tag here i tried to fetch data with class
         * = tableData in table tag you can fetch using id or other attribute
         * rowCount variable to create row for excel sheet
         */        Cell cell;
        for (Element table : doc.select("table[class=dataintable]")) {
            Elements trs = table.select("tr");
            int rowAmount = trs.size();
            int colAmount = table.select("tbody tr:eq(0)").select("td").size();
            List<List<Boolean>> lists = initLists(rowAmount, colAmount);
            // loop through all tr of tablefor (Element row : trs) {
                // create row for each
                header = sheet.createRow(rowCount);
                // loop through all tag of
                Elements ths = row.select("th");
                int colCount = 0;
                if (ths.size() != 0) {
                    List<Boolean> iList = lists.get(rowCount);
                    int index = 0;
                    for (int colIndex = 0; colIndex < colAmount; ++colIndex) {
                        if (iList.get(colIndex)) {
                            // set header style
                            cell = header.createCell(colCount);
                            cell.setCellValue("");
                            cell.setCellStyle(headerStyle);
                        } else {
                            Element element = ths.get(index++);
                            // set header style
                            cell = header.createCell(colCount);
                            cell.setCellValue(element.text());
                            cell.setCellStyle(headerStyle);

                            String rowspan = element.attr("rowspan").trim();
                            String colspan = element.attr("colspan").trim();
                            if (rowspan.length() != 0) {
                                Integer rowspanInt = Integer.parseInt(rowspan);
                                Integer colspanInt = Integer.parseInt(colspan);
                                int firstRow = rowCount;
                                int lastRow = firstRow + rowspanInt - 1;
                                int firstCol = colCount;
                                int lastCol = colCount + colspanInt - 1;
                                sheet.addMergedRegion(new CellRangeAddress(
                                        firstRow, lastRow, firstCol, lastCol));

                                for (int i = firstRow; i <= lastRow; ++i) {
                                    for (int j = firstCol; j <= lastCol; ++j) {
                                        lists.get(i).set(j, true);
                                    }
                                }
                            } else {
                                lists.get(rowCount).set(colCount, true);
                            }
                        }
                        colCount++;
                    }
                }

                // now loop through all td tag
                Elements tds = row.select("td:not([rowspan])");
                colCount = 0;
                for (Element element : tds) {
                    // create cell for each tag
                    cell = header.createCell(colCount);
                    cell.setCellValue(element.text());
                    cell.setCellStyle(bodyStyle);
                    colCount++;
                }
                rowCount++;

            }
        }
        String filepath = "g:/export.xls";
        wb.write(new FileOutputStream(new File(filepath)));
        System.out.println("成功导出。" + filepath);
    }

    privatestatic List<List<Boolean>> initLists(int rowAmount, int colAmount) {
        List<List<Boolean>> lists = new ArrayList<List<Boolean>>();
        for (int i = 0; i < rowAmount; ++i) {
            List<Boolean> list = new ArrayList<Boolean>();
            for (int j = 0; j < colAmount; ++j) {
                list.add(false);
            }
            lists.add(list);
        }
        return lists;
    }
}
<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.10.1</version></dependency>

以上就介绍了Java导出Excel,包括了方面的内容,希望对Java教程有兴趣的朋友有所帮助。

本文网址链接:http://www.codes51.com/article/detail_117478.html

相关图片

相关文章