Poi export templates using fixed excel, the emergence of Chinese characters can not automatically set the line width

Because at work, you may encounter different problems for different reasons, I will share a pit encountered

Because I want the name of the database column names as part of a field of Excel, so you need to export Chinese characters, then fill in the data corresponding Chinese characters below.

It is because this part of the Chinese characters, in the process of using the Export table, all the characters are crowded together,

We did not achieve the results we want in here so I made the following settings:

Dependence is introduced:

<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.16</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.16</version>
        </dependency>

Test code:

package exercise.demo;

import org.apache.poi.hssf.usermodel.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.FileOutputStream;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {

    @Test
    public  void excelUtils() {
        /**
         * @see <a href="http://poi.apache.org/hssf/quick-guide.html #NewWorkbook "> the For More </a>
          * / 
        // Create a new Excel workbook 
        HSSFWorkbook Workbook = new new HSSFWorkbook (); 

        // work in Excel book build a worksheet that is named the default value can also be specified sheet name 
        HSSFSheet sheet = workbook.createSheet ();
         // HSSFSheet sheet = workbook.createSheet ( "SheetName"); 

        // for formatting cells data 
        HSSFDataFormat the format = workbook.createDataFormat (); 

        // .. creates a new line (row), and cells (cell) into which the line number counted from 0 
        HSSFRow sheet.createRow row = (( Short ). 1 ) ; 

        // set the font 
        HSSFFont font =workbook.createFont (); 
        font.setFontHeightInPoints (( Short ) 20); // font height 
        font.setColor (HSSFFont.COLOR_RED); // font color 
        font.setFontName ( "black body"); // font 
        font.setBoldweight (HSSFFont .BOLDWEIGHT_BOLD); // width 
        font.setItalic ( to true ); // whether italic
 //         font.setStrikeout (to true); // whether scribing 

        // set the cell type 
        HSSFCellStyle CellStyle = workbook.createCellStyle (); 
        CellStyle .setFont (font); 
        CellStyle .setAlignment (HSSFCellStyle.ALIGN_CENTER); // horizontal layout: center
        cellStyle.setWrapText ( to true ); 

        // add cell comment
         // create HSSFPatriarch objects, all annotated HSSFPatriarch containers. 
        HSSFPatriarch patr = sheet.createDrawingPatriarch ();
         // define the size and position of the annotation, the document detailed 
        HSSFComment comment = patr.createComment ( new new HSSFClientAnchor (0, 0, 0, 0, ( Short ). 4, 2, ( Short ). 6,. 5 ));
         // set the annotation content 
        comment.setString ( new new HSSFRichTextString ( "in the POI can add comments ! " ));
         // set annotation of the mouse to move to the cell can be seen in the status bar when the content. 
        comment.setAuthor (" Xuys. " ); 

//        // Create cell 
        HSSFCell Cell row.createCell = (( Short ). 1 ); 
        String [] = columMethodNms new new String [] { "Householder *", "* house number" }; 

        for ( int J = 0; J < columMethodNms.length; J ++ ) { 
            Cell = row.createCell (J);
 //             cell.setCellStyle (); 
            cell.setCellValue (columMethodNms [J]); 

this part where the code characters can change a line width
sheet.setColumnWidth (j, columMethodNms [J] .getBytes () length.
* 256 ); } the try { a FileOutputStream FILEOUT = new new FileOutputStream("G:/3.xls"); workbook.write(fileOut); fileOut.close(); } catch (Exception e) { System.out.println(e.toString()); } } }

This part of the code after I tested, is the normal operation of workers.

Or you try to introduce this method into this class.

 private static void reformatSheetWidth(XSSFSheet sheet, String[] columMethodNms) {
        for (int columnNum = 0; columnNum < columMethodNms.length; columnNum++) {
            int columnWidth = sheet.getColumnWidth(columnNum) / 256;
            for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {
                XSSFRow currentRow;
                //当前行未被使用过
                if (sheet.getRow(rowNum) == null) {
                    currentRow = sheet.createRow(rowNum);
                } else {
                    currentRow = sheet.getRow(rowNum);
                }

                if (currentRow.getCell(columnNum) != null) {
                    XSSFCell currentCell = currentRow.getCell(columnNum);
                    if (currentCell.getCellType() == XSSFCell.CELL_TYPE_STRING) {
                        int length = currentCell.getStringCellValue().getBytes().length;
                        if (columnWidth < length) {
                            columnWidth = length;
                        }
                    }
                }
            }
            sheet.setColumnWidth(columnNum, columnWidth * 256);
        }
    }

Both methods can be used for testing, I used this method in the project.

Guess you like

Origin www.cnblogs.com/qingmuchuanqi48/p/11286119.html
Recommended