3

我使用 java 在 excel 中编写了一些数据。我需要将特定单元格包装为 wrap。有谁能告诉我包装文本的语法是什么以及如何使用它?

4

4 回答 4

9

参考Apache POI

Workbook wb = new XSSFWorkbook();  
Sheet sheet = wb.createSheet();

Row row = sheet.createRow(2);
Cell cell = row.createCell(2);
cell.setCellValue("Use \n with word wrap on to create a new line");

CellStyle cs = wb.createCellStyle();
cs.setWrapText(true);   //Wrapping text
cell.setCellStyle(cs);
于 2013-09-30T07:15:06.637 回答
2

在 jxl 中,你必须使用 setWrap(true):

WritableCellFormat cellFormat = new WritableCellFormat();
cellFormat.setWrap(true);

sheet.addCell(new Label(1, 1, "A simple test message", cellFormat));    
sheet.addCell(new Label(1, 2, "An other text", cellFormat));

编辑:

要添加粗体单元格:

WritableFont cellFont = new WritableFont(WritableFont.COURIER, 16);
cellFont.setBoldStyle(WritableFont.BOLD);

WritableCellFormat cellFormatBold = new WritableCellFormat(cellFont);
sheet.addCell(new Label(1, 2, "An other text", cellFormatBold));
于 2013-09-30T08:24:37.563 回答
0

要读取 excel 文件,您可以使用

  • 阿帕奇

    其中 excel 数据将读取为

     Workbook wb=WorkbookFactory.create(new FileInputStream(filePath));
     CellStyle cs=wb.createCellStyle();
     cs.setWrapText(true);
    
     Cell cell=wb.getSheet(0).getRow(0).getCell();
     cell.setCellStyle(cs);
    
于 2013-09-30T07:20:38.643 回答
0

自动文字换行:

public static void main(String []args){
String  str="Baraitha Katra Muzaffarpur RajaPuri {vimal} Ajmetigaet 8433221 vikaspuri New Delhi Faridabad India(.SSS)";
    String [] strArray=str.split(" ");
    ArrayList StrList=new ArrayList();
    String lineStr="";
    int start =0;
    for(int i=0;i<strArray.length;i++){
         lineStr=lineStr+strArray[i]+" ";
        if(lineStr.length()>30){
            String finalStr="";
            for(int j=start;j<i ;j++){
                finalStr=finalStr+strArray[j]+" ";
            }
            StrList.add(finalStr);
            lineStr="";
            start=i;
            i=i-1;              
        }       
    }
    StrList.add(lineStr);
System.out.println("Number of Lines-"+StrList.size());
for(int j=0;j<StrList.size();j++){
    System.out.println(StrList.get(j));

   }        
}
于 2016-08-17T10:48:57.403 回答