POI 生成Excel时,怎么限制 单元格输入长度的限制
答案:2 悬赏:10 手机版
解决时间 2021-01-25 03:19
- 提问者网友:嘚啵嘚啵
- 2021-01-24 12:33
POI 生成Excel时,怎么限制 单元格输入长度的限制
最佳答案
- 五星知识达人网友:舊物识亽
- 2021-01-24 13:49
import java.io.fileoutputstream; import java.io.ioexception; import org.apache.poi.hssf.usermodel.hssfcell; import org.apache.poi.hssf.usermodel.hssfcellstyle; import org.apache.poi.hssf.usermodel.hssfrow; import org.apache.poi.hssf.usermodel.hssfsheet; import org.apache.poi.hssf.usermodel.hssfworkbook; import org.apache.poi.hssf.util.region; public class exceltest { 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(); } } }
全部回答
- 1楼网友:拾荒鲤
- 2021-01-24 14:34
在开发中经常需要用到对Excel文件的操作,POI生成excel实现自动调整行高的代码如下:
import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFComment;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
public class PoiCreateExcelTest {
public static void main(String[] args) {
// 创建新的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(
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯