关于java写office Excel 2007的方法

说明:
① [color=red]writeRecord(String[] strings)[/color] 当传一个数组参数过来的时候,数组的每一项写到行的每个列的格里面.示例参照注释掉的main方法.
② [color=red]writeRecord(String arg)[/color] 这一个字符串是行的一个列格,如新起一行需要调用[color=red]newLine()[/color]方法,示例参照main方法.
很少写文章有什么语意不通的地方还望谅解 :wink: ,如有问题再行沟通.
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
* ExcelWriter Description
*
*/
public class ExcelWriter {

private OutputStream outputStream = null;
private String sheetName="sheet1";
private XSSFWorkbook workbook;

public String getSheetName() {
return sheetName;
}

public void setSheetName(String sheetName) {
this.sheetName = sheetName;
}

/**
* @param fileOutputStream
* @param forName
* @throws IOException
*/
public ExcelWriter(String filePath) throws IOException {
workbook = new XSSFWorkbook();
this.outputStream = new FileOutputStream(filePath);
}

public void close() {
try {
workbook.write(outputStream);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

public void writeRecord(String[] strings) throws IOException {
int sheetNum = workbook.getNumberOfSheets();
XSSFSheet sheet = null;
if (sheetNum == 0) {
sheet = workbook.createSheet();
workbook.setSheetName(0, sheetName);
} else {
sheet = workbook.getSheetAt(0);
}
XSSFCell cell = null;
XSSFCellStyle cs = null;
XSSFRichTextString xssfValue = null;
int rowNum = sheet.getLastRowNum();
rowNum++;
XSSFRow row = sheet.createRow(rowNum);
for (int n = 0; n < strings.length; n++) {// 写出列
cell = row.createCell(n);
cs = cell.getCellStyle();
cs.setFillPattern(XSSFCellStyle.ALIGN_GENERAL);
cs.setWrapText(true);
cs.setVerticalAlignment(XSSFCellStyle.ALIGN_LEFT);
cell.setCellStyle(cs);
cell.setCellType(XSSFCell.CELL_TYPE_STRING);
xssfValue = new XSSFRichTextString(strings[n]);
cell.setCellValue(xssfValue);
}
}

/*
* public static void main(String[] args) throws IOException { ExcelWriter
* writer = null; try { writer = new
* ExcelWriter("D:\\test\\test.xlsx"); } catch
* (FileNotFoundException e) { System.out.println("112"); }
* writer.setSheetName("sheet1"); List<String[]> list = new
* ArrayList<String[]>(); list.add(new String[] { "name", "张三",
* "11" }); list.add(new String[] { "age", "34", "22" });
* list.add(new String[] { "sex", "man", "33" });
* for (int i = 0; i < list.size(); i++)
* { String[] arg = list.get(i); writer.writeRecord(arg); } writer.close();
* }
*/

public static void main(String[] args) throws IOException {
ExcelWriter writer = null;
try {
writer = new ExcelWriter("D:\\test\\test.xlsx");
} catch (FileNotFoundException e) {
}
writer.setSheetName("sheet1");
List<String> list = new ArrayList<String>();
list.add("aaa");
list.add("bbb");
list.add("ccc");
list.add("\n");
list.add("ddd");
list.add("");
list.add("eee");
for (int i = 0; i < list.size(); i++) {
String arg = list.get(i);
writer.writeRecord(arg);
}
writer.close();
}

public void newLine() {
writeRecord("\n");
}

/**
* @param arg
*/
public void writeRecord(String arg) {
int sheetNum = workbook.getNumberOfSheets();
XSSFSheet sheet = null;
if (sheetNum == 0) {
sheet = workbook.createSheet();
workbook.setSheetName(0, sheetName);
} else {
sheet = workbook.getSheetAt(0);
}
XSSFCell cell = null;
XSSFCellStyle cs = null;
XSSFRichTextString xssfValue = null;
XSSFRow row = null;
int rowNum = sheet.getLastRowNum();
if (rowNum == -1) {
rowNum++;
row = sheet.createRow(rowNum);
} else if (arg.equals("\n")) {
rowNum++;
row = sheet.createRow(rowNum);
return;
} else {
row = sheet.getRow(rowNum);
}
int colNum = row.getLastCellNum();
if (colNum == -1) {
colNum++;
}
cell = row.createCell(colNum);
colNum++;
cs = cell.getCellStyle();
cs.setFillPattern(XSSFCellStyle.ALIGN_GENERAL);
cs.setWrapText(true);
cs.setVerticalAlignment(XSSFCellStyle.ALIGN_LEFT);
cell.setCellStyle(cs);
cell.setCellType(XSSFCell.CELL_TYPE_STRING);
xssfValue = new XSSFRichTextString(arg);
cell.setCellValue(xssfValue);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值