자바,poi 로 엑셀 내 보 내기
package zy.demo.test;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.util.Region;
import org.hibernate.mapping.Array;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ToExcel {
public static String xlsFile = "d:/test.xls"; // Excel
// cell
@SuppressWarnings("deprecation")
public static void main(String[] agrs) throws IOException {
HSSFWorkbook workbook = new HSSFWorkbook(); //
HSSFSheet sheet = workbook.createSheet(" "); //
// firstSheet
// , UTF_16
workbook.setSheetName(0, "firstSheet");
// , 8
HSSFRow row = sheet.createRow((short) 0);
HSSFRow row1 = sheet.createRow((short) 1);
//
sheet.setColumnWidth((short) 0, (short) 4000);
sheet.setColumnWidth((short) 1, (short) 4000);
sheet.setColumnWidth((short) 2, (short) 4000);
sheet.setColumnWidth((short) 3, (short) 4000);
sheet.setColumnWidth((short) 4, (short) 4000);
sheet.setColumnWidth((short) 5, (short) 4000);
sheet.setColumnWidth((short) 6, (short) 5000);
sheet.setColumnWidth((short) 7, (short) 5000);
sheet.setColumnWidth((short) 8, (short) 5000);
//
HSSFCell cell1 = row1.createCell((short) 0);
//
cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
// , UTF_16。.....
//
cell1.setCellValue(" ");
String[] head = { " ", " 1", " 2", " 3", " 4", " 5", " 6", " 7", " 8" };
for (int i = 0; i < head.length; i++) {
HSSFCell cell = row.createCell((short) i);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(head[i]);
}
ArrayList<String> list = new ArrayList();
list.add("1");
list.add("2");
list.add("3");
list.add("4");
for(int i=0;i<list.size();i++){
//
HSSFRow row2 = sheet.createRow((short)(i+2));
//
HSSFCell cell = row2.createCell((short)0);
cell.setCellValue(list.get(0));
}
FileOutputStream fOut = new FileOutputStream(xlsFile);
;
try {
fOut = new FileOutputStream(xlsFile);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
workbook.write(fOut);
fOut.flush();
System.out.println(" ...");
// Excel
FileInputStream fIn = new FileInputStream(xlsFile);
HSSFWorkbook readWorkBook = new HSSFWorkbook(fIn);
HSSFSheet readSheet = readWorkBook.getSheet("firstSheet");
HSSFRow readRow = readSheet.getRow(0);
HSSFCell readCell = readRow.getCell((short) 0);
HSSFCell readCell1 = readRow.getCell((short) 1);
System.out.println(" :" + readCell.getStringCellValue());
System.out.println(readCell1.getStringCellValue());
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.