Java利用POI生成Excel強制換行
使用POI創建一個簡單的 myXls.xls 文件
常用的包為 org.apache.poi.hssf.usermodel.*;
例子:
import java.io.*;
import org.apache.poi.hssf.usermodel.*;
public class ZoomSheet {
public ZoomSheet() {
}
public static void main(String args[])
throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
}
}
類:
HSSFWorkbook 創建 xls 的對象; HSSFWorkbook hw = new HSSFWorkbook();
設置分區顯示; hw.setRepeatingRowsAndColumns(sheet的index, 行, 列, 行, 列);
HSSFSheet 創建 xls 中的sheet(工作表); HSSFSheet sheet = hw.createSheet("sheet1"); sheet1 是 sheet 的名稱 可缺省
設置列高; sheet.setColumnWidth((short)short, (short)short);
HSSFRow 創建 xls 中的行; HSSFRow row = sheet.createRow(0); 0 表示第一行
設置行高; row.setHeight((short)short);
HSSFFont 創建 xls 中的字體; HSSFFont font = hw.createFont();
設定字體大小; font.setFontHeightInPoints((short)54);
設定為斜體; font.setItalic(true);
設定文字刪除線; font.setStrikeout(true);
HSSFCellStyle 設定單元格風格; HSSFCellStyle style = wb.createCellStyle();
加入字體; style.setFont(font);
HSSFCell 設定單元格; HSSFCell cell = row.createCell((short)0);
單元格水平對齊方式; style.setAlignment(align); //單元格水平 0 普通 1 左對齊 2 居中 3 右對齊 4 填充 5 正當 6 居中選擇
單元格垂直對齊方式; style.setVerticalAlignment(align); //單元格垂直 0 居上 1 居中 2 居下 3 正當
單元格下邊框為細線; style.setBorderBottom((short)short);
同上一命令一同使用,設置顏色; style.setBottomBorderColor((short)short);
單元格左邊框; style.setBorderLeft((short)short);
style.setLeftBorderColor((short)short);
單元格右邊框; style.setBorderRight((short)short);
style.setRightBorderColor((short)short);
單元格上邊框; style.setBorderTop((short)short);
style.setTopBorderColor((short)short);
單元格字符編號(中文); cell.setEncoding(HSSFCell.ENCODING_UTF_16); //中文
單元格顯示的值; cell.setCellValue("中醫葯"); 值的類型有:double,int,String,Date,boolean
單元格背景色; style.setFillForegroundColor((short)short);
圖案類型; style.setFillPattern((short)short);
單元格合並; sheet.addMergedRegion(new Region(行, (short)列, 行, (short)列));
單元格風格加入; cell.setCellStyle(style);
打印設置
引入包 import org.apache.poi.hssf.usermodel.HSSFPrintSetup;
創建打印設置對象 HSSFPrintSetup hps = hs.getPrintSetup();
設置A4紙 hps.setPaperSize((short)9);
將頁面設置為橫向打印模式 hps.setLandscape(true);
設置打印頁面為水平居中 sheet.setHorizontallyCenter(true);
設置打印頁面為垂直居中 sheet.setVerticallyCenter(true);
網上找到的文章都是說在excel里的文字里加上\n,\n\r,\r\n之類,反正各種各樣的都有,更奇怪的是還有人說在單元格里加上<br>
后來我試過用\r后的效里是生成的文件里,你用打開時,並不會換行,如果你用鼠標在單元格里點一下之后就會自動換行。
可以通過如下方式進行,
1. 首先在需要強制換行的單元格里使用poi的樣式,並且把樣式設定為自動換行
# HSSFCellStyle cellStyle=workbook.createCellStyle();
# cellStyle.setWrapText(true);
# cell.setCellStyle(cellStyle);
2. 其次是在需要強制換行的單元格,使用\就可以實再強制換行
1. HSSFCell cell = row.createCell((short)0);
2. cell.setCellStyle(cellStyle); cell.setCellValue(new HSSFRichTextString("hello\r\n world!"));
這樣就能實現強制換行,
換行后的效里是單元格里強制換行
hello
world!
public class ExcelTest {
/**
* @param args
*/
public static void main(String[] args) throws IOException {
try {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFCellStyle style = wb.createCellStyle(); // 樣式對象
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平
/**字體begin*/
style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
//背景顏色
// style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
// style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
// style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
// style.setBorderRight(HSSFCellStyle.BORDER_THIN);
// style.setBorderTop(HSSFCellStyle.BORDER_THIN);
// style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//生成一個字體
HSSFFont font=wb.createFont();
font.setColor(HSSFColor.BLACK.index);//HSSFColor.VIOLET.index //字體顏色
font.setFontHeightInPoints((short)12);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //字體增粗
//把字體應用到當前的樣式
style.setFont(font);
/**字體end*/
HSSFRow row = sheet.createRow((short) 0);
HSSFRow row2 = sheet.createRow((short) 1);
// 四個參數分別是:起始行,起始列,結束行,結束列
sheet.addMergedRegion(new Region(0, (short) 0, 5, (short) 0));
HSSFCell ce = row.createCell((short) 0);
ce.setCellValue("項目\\日期"); // 表格的第一行第一列顯示的數據
ce.setCellStyle(style); // 樣式,居中
int num = 0;
for (int i = 0; i < 9; i++) { // 循環9次,每一次都要跨單元格顯示
// 計算從那個單元格跨到那一格
int celln = 0;
int celle = 0;
if (i == 0) {
celln = 0;
celle = 1;
} else {
celln = (i * 2);
celle = (i * 2 + 1);
}
// 單元格合並
// 四個參數分別是:起始行,起始列,結束行,結束列
sheet.addMergedRegion(new Region(0, (short) (celln + 1), 0,
(short) (celle + 1)));
HSSFCell cell = row.createCell((short) (celln + 1));
cell.setCellValue("merging" + i); // 跨單元格顯示的數據
cell.setCellStyle(style); // 樣式
// 不跨單元格顯示的數據,如:分兩行,上一行分別兩格為一格,下一行就為兩格,“數量”,“金額”
HSSFCell cell1 = row2.createCell((short) celle);
HSSFCell cell2 = row2.createCell((short) (celle + 1));
cell1.setCellValue("數量");
cell1.setCellStyle(style);
cell2.setCellValue("金額");
cell2.setCellStyle(style);
num++;
}
// 在后面加上合計百分比
// 合計 在最后加上,還要跨一個單元格 //四個參數分別是:起始行,起始列,結束行,結束列
sheet.addMergedRegion(new Region(0, (short) (2 * num + 1), 0,
(short) (2 * num + 2)));
HSSFCell cell = row.createCell((short) (2 * num + 1));
cell.setCellValue("合計");
cell.setCellStyle(style);
HSSFCell cell1 = row2.createCell((short) (2 * num + 1));
HSSFCell cell2 = row2.createCell((short) (2 * num + 2));
cell1.setCellValue("數量");
cell1.setCellStyle(style);
cell2.setCellValue("金額");
cell2.setCellStyle(style);
// 百分比 同上
sheet.addMergedRegion(new Region(0, (short) (2 * num + 3), 0,
(short) (2 * num + 4)));
HSSFCell cellb = row.createCell((short) (2 * num + 3));
cellb.setCellValue("百分比");
cellb.setCellStyle(style);
HSSFCell cellb1 = row2.createCell((short) (2 * num + 3));
HSSFCell cellb2 = row2.createCell((short) (2 * num + 4));
cellb1.setCellValue("數量");
cellb1.setCellStyle(style);
cellb2.setCellValue("金額");
cellb2.setCellStyle(style);
//輸出一些數據 然后再輸出表頭
FileOutputStream fileOut = new FileOutputStream("D://workbook.xls");
wb.write(fileOut);
fileOut.close();
System.out.print("OK");
} catch (Exception ex) {
ex.printStackTrace();
}
}
//設置單元格字體顏色
import Java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
public class test {
public static void main(String[] args) {
Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet();
Cell cell = sheet.createRow(0).createCell(0);
CreationHelper helper = workbook.getCreationHelper();
RichTextString str = helper.createRichTextString("a\nb\nc\nd\ne\n");// 在這里使用\n表示回車
Font[] fonts = new Font[5];
fonts[0] = workbook.createFont();
fonts[0].setColor(HSSFColor.YELLOW.index);
fonts[1] = workbook.createFont();
fonts[1].setColor(HSSFColor.RED.index);
fonts[2] = workbook.createFont();
fonts[2].setColor(HSSFColor.BLUE.index);
fonts[3] = workbook.createFont();
fonts[3].setColor(HSSFColor.ROSE.index);
fonts[4] = workbook.createFont();
fonts[4].setColor(HSSFColor.BLACK.index);
for (int i = 0; i < 5; i++) {
str.applyFont(i * 2, (i + 1) * 2, fonts[i]);
}
cell.setCellValue(str);
try {
FileOutputStream out = new FileOutputStream(new File("d:\\1.xls"));
workbook.write(out);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
