java poi导出excel要双击才显示换行? java poi读取excel文件
java用poi往excel里写数据遇到换行问题
String[] arr = new String[] { "a", "b", "c", "d", "a", "b", "c", "d", "a", "b", "c", "d" };
List<String> list = Arrays.asList(arr);
if (list != null) {
StringBuffer buff = new StringBuffer();
int colIndex = 0;// 用来标识列下标
for (int i = 0; i < list.size(); i++) {
if (i != 0 && i % 3 == 0) {// 不是第一个下标并且能被3整除,也就是4个一个轮回
buff.append(list.get(i));
rows.createCell(colIndex++).setCellValue(new String(buff));
buff = new StringBuffer();// 清空buff内容
} else {
buff.append(list.get(i));
}
}
}
java导出excel怎让单元格的内容换行
模板设置单元格自动换行即可 2007 选中单元格——自动换行按钮 2003 选中单元格右键单元格格式 第二个选项卡设置
poi导出excel表格,在导出的时候能不能设置内容换行
在开发中经常需要用到对Excel文件的操作,POI生成excel实现自动调整行高的代码如下:
import java.io.FileOutputStream;
import .apache.poi.hssf.usermodel.HSSFCell;
import .apache.poi.hssf.usermodel.HSSFCellStyle;
import .apache.poi.hssf.usermodel.HSSFFont;
import .apache.poi.hssf.usermodel.HSSFRow;
import .apache.poi.hssf.usermodel.HSSFSheet;
import .apache.poi.hssf.usermodel.HSSFWorkbook;
import .apache.poi.hssf.usermodel.HSSFRichTextString;
import .apache.poi.hssf.usermodel.HSSFDataFormat;
import .apache.poi.hssf.usermodel.HSSFComment;
import .apache.poi.hssf.usermodel.HSSFPatriarch;
import .apache.poi.hssf.usermodel.HSSFClientAnchor;
public class PoiCreateExcelTest {
public static void main(String[] args) {
/**
* @see For more
*/
// 创建新的Excel 工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
// 在Excel工作簿中建一工作表,其名为缺省值, 也可以指定Sheet名称
HSSFSheet sheet = workbook.createSheet();
//HSSFSheet sheet = workbook.createSheet("SheetName");
// 用于格式化单元格的数据
HSSFDataFormat format = workbook.createDataFormat();
// 创建新行(row),并将单元格(cell)放入其中. 行号从0开始计算.
HSSFRow row = sheet.createRow((short) 1);
// 设置字体
HSSFFont font = workbook.createFont();
font.setFontHeightInPoints((short) 20); //字体高度
font.setColor(HSSFFont.COLOR_RED); //字体颜色
font.setFontName("黑体"); //字体
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //宽度
font.setItalic(true); //是否使用斜体
// font.setStrikeout(true); //是否使用划线
// 设置单元格类型
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平布局:居中
cellStyle.setWrapText(true);
// 添加单元格注释
// 创建HSSFPatriarch对象,HSSFPatriarch是所有注释的容器.
HSSFPatriarch patr = sheet.createDrawingPatriarch();
// 定义注释的大小和位置,详见文档
HSSFComment comment = patr.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short)4, 2, (short) 6, 5));
// 设置注释内容
comment.setString(new HSSFRichTextString("可以在POI中添加注释!"));
// 设置注释作者. 当鼠标移动到单元格上是可以在状态栏中看到该内容.
comment.setAuthor("Xuys.");
// 创建单元格
HSSFCell cell = row.createCell((short) 1);
HSSFRichTextString hssfString = new HSSFRichTextString("Hello World!");
cell.setCellValue(hssfString);//设置单元格内容
cell.setCellStyle(cellStyle);//设置单元格样式
cell.setCellType(HSSFCell.CELL_TYPE_STRING);//指定单元格格式:数值、公式或字符串
cell.setCellComment(comment);//添加注释
//格式化数据
row = sheet.createRow((short) 2);
cell = row.createCell((short) 2);
cell.setCellValue(11111.25);
cellStyle = workbook.createCellStyle();
cellStyle.setDataFormat(format.getFormat("0.0"));
cell.setCellStyle(cellStyle);
row = sheet.createRow((short) 3);
cell = row.createCell((short) 3);
cell.setCellValue(9736279.073);
cellStyle = workbook.createCellStyle();
cellStyle.setDataFormat(format.getFormat("#,##0.0000"));
cell.setCellStyle(cellStyle);
sheet.autoSizeColumn((short)0); //调整第一列宽度
sheet.autoSizeColumn((short)1); //调整第二列宽度
sheet.autoSizeColumn((short)2); //调整第三列宽度
sheet.autoSizeColumn((short)3); //调整第四列宽度
try {
FileOutputStream fileOut = new FileOutputStream("C:/3.xls");
workbook.write(fileOut);
fileOut.close();
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
poi生成表格不能自动换行
POI生成excel表格,如何合并单元格
Java代码
import java.io.FileOutputStream;
import java.io.IOException;
import .apache.poi.hssf.usermodel.HSSFCell;
import .apache.poi.hssf.usermodel.HSSFCellStyle;
import .apache.poi.hssf.usermodel.HSSFRow;
import .apache.poi.hssf.usermodel.HSSFSheet;
import .apache.poi.hssf.usermodel.HSSFWorkbook;
import .apache.poi.hssf.util.Region;
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);// 水平
HSSFRow row = sheet.createRow((short) 0);
HSSFRow row2 = sheet.createRow((short) 1);
sheet.addMergedRegion(new Region(0, (short) 0, 1, (short) 0));
HSSFCell ce = row.createCell((short) 0);
ce.setEncoding(HSSFCell.ENCODING_UTF_16);// 中文处理
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.setEncoding(HSSFCell.ENCODING_UTF_16);
cell1.setCellValue("数量");
cell1.setCellStyle(style);
cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
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.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("合计");
cell.setCellStyle(style);
HSSFCell cell1 = row2.createCell((short) (2 * num + 1));
HSSFCell cell2 = row2.createCell((short) (2 * num + 2));
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
cell1.setCellValue("数量");
cell1.setCellStyle(style);
cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
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.setEncoding(HSSFCell.ENCODING_UTF_16);
cellb.setCellValue("百分比");
cellb.setCellStyle(style);
HSSFCell cellb1 = row2.createCell((short) (2 * num + 3));
HSSFCell cellb2 = row2.createCell((short) (2 * num + 4));
cellb1.setEncoding(HSSFCell.ENCODING_UTF_16);
cellb1.setCellValue("数量");
cellb1.setCellStyle(style);
cellb2.setEncoding(HSSFCell.ENCODING_UTF_16);
cellb2.setCellValue("金额");
cellb2.setCellStyle(style);
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
System.out.print("OK");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}