我需要使用java将文本包装在excel中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19088357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:58:57  来源:igfitidea点击:

I need to wrap the text in excel using java

java

提问by Arun Kumar

I write some data in excel using java.I need to wrap the particular cell as wrap.can any tell me what is the syntax for wrap text and how can I use it?

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

采纳答案by fluminis

In jxl, you have to use setWrap(true):

在 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));

Edit:

编辑:

To add a bold cell:

添加粗体单元格:

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));

回答by Woody

Refer Apache POI

参考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);

回答by kark

To read an excel file you can use

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

  • Apache poi

    In which the excel data's will read as

     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);
    
  • 阿帕奇poi

    其中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);
    

回答by user6725910

Auto Text Wrap:

自动文本换行:

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));

   }        
}