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