Java导出Excel

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;

public class ExportExcel {

    public static void main(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 table
            for (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);
    }

    private static 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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值