用Java写入Excel

时间:2018-01-03 17:39:19

标签: java excel apache-poi

我在论坛上搜索没有占上风,所以我在这里。

我正在尝试实现一种写入Excel的方法,但每次只会破坏文件。这是我的代码片段:

public static XSSFSheet printToExcel (ArrayList<String> al, Workbook wb, XSSFSheet s, FileWriter fw) throws FileNotFoundException, IOException {
    int numOfRows = s.getPhysicalNumberOfRows();

    for (int i = 0; i <= numOfRows; i++ ) {
        XSSFRow row = s.createRow(i);
        XSSFCell cell = row.createCell(i);
        String text = al.get(i);
        cell.setCellValue(text);
        fw.write(text);
        fw.close();
    }
    return s;
}

希望你们都能帮助我。我大多是自学成才,所以任何建议都会受到赞赏。

1 个答案:

答案 0 :(得分:0)

这是我在另一个背景下写的一个例子,但它会帮助你开始

 import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.xssf.usermodel.*;

import java.awt.*;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;


public class WriteExcelBasic {
    public static void main(String[] args) throws IOException {

        String excelFileName = "/Users/home/Test3.xlsx";
        FileOutputStream fos = new FileOutputStream(excelFileName);


        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFCellStyle style = wb.createCellStyle();

        XSSFSheet sheet = wb.createSheet("sheet");
 XSSFFont urlFont = wb.createFont();// changed lines
 urlFont.setFontHeight((short)(9*20));
       // urlFont.setUnderline((byte)10);
        //urlFont.setBold(true);
       urlFont.setFontName("Arial");
        urlFont.setItalic(true);
      urlFont.setColor(new XSSFColor(Color.BLUE)); // changed lines



        style.setFont(urlFont);
        for (int r = 0; r < 3; r++) {
            XSSFRow row = sheet.createRow(r);


            for (int c = 0; c < 3; c++) {
                XSSFCell cell = row.createCell(c);


                Hyperlink link = wb.getCreationHelper().createHyperlink(HyperlinkType.URL);
                String ss = "http://news.google.com/news/headlines?ned=us&hl=en";
                //String ss = "swasdqde";
               link.setAddress(ss);
                cell.setHyperlink(link);
                cell.setCellValue(ss);
                if(r == 1) {
                    System.out.println("In yellow");
                    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
                    style.setFillForegroundColor(new XSSFColor(Color.YELLOW));
                } else {
                    System.out.println("In red");
                    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
                    style.setFillForegroundColor(new XSSFColor(Color.RED));
                }
                cell.setCellStyle(style);


            }
        }

        try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
            wb.write(baos);
            byte[] myByteArray = baos.toByteArray();
            fos.write(myByteArray);
            fos.flush();
        }
        finally {
            wb.close();
            fos.close();
        }
    }
}