org.beangle.commons.transfer.excel.ExcelItemWriterTest.java Source code

Java tutorial

Introduction

Here is the source code for org.beangle.commons.transfer.excel.ExcelItemWriterTest.java

Source

/* Copyright c 2005-2012.
 * Licensed under GNU  LESSER General Public License, Version 3.
 * http://www.gnu.org/licenses
 */
package org.beangle.commons.transfer.excel;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.testng.Assert;
import org.testng.annotations.Test;

@Test
public class ExcelItemWriterTest {

    public void testWrite() throws IOException {
        File file = new File("src/test/resources/tmp.xls");
        if (!file.exists()) {
            file.createNewFile();
        }
        ExcelItemWriter writer = new ExcelItemWriter(new FileOutputStream(file));
        writer.writeTitle("?", new String[] { "??", "", "??", "?" });
        writer.write(new String[] { "", "", "xxxx", "" });
        writer.close();
    }

    @Test(dependsOnMethods = { "testWrite" })
    public void testRead() throws Exception {
        File file = new File("src/test/resources/tmp.xls");
        FileInputStream in = new FileInputStream(file);
        HSSFWorkbook wb = new HSSFWorkbook(in);
        HSSFCell cell = wb.getSheetAt(0).getRow(0).getCell(0);
        // HSSFFont f = cell.getCellStyle().getFont(wb);
        // Assert.assertEquals(f.getBoldweight(), HSSFFont.BOLDWEIGHT_BOLD);
        Assert.assertEquals(cell.getCellStyle().getAlignment(), HSSFCellStyle.ALIGN_CENTER);
        in.close();
        file.delete();
    }
}