HSSF font underline

Source: Internet
Author: User

It took a long time to set the underline. Finally, I found POI. The English is not good... I tried it several times to understand it ..... The Chinese are trying to turn all technical documents into Chinese .... Let foreigners also experience the profound and profound Chinese ..

This is the result I want in my project, but below I will give a small example of self-writing that can run successfully independently. If you don't understand it, try it. Of course, you can contact me...


Related code:

Boolean result = false; // Step 1: Create a webbook, corresponding to an Excel file HSSFWorkbook wb = new HSSFWorkbook (); // Step 2: Add a sheet to the webbook, corresponding to the sheet in the Excel file. createSheet ("sheet1"); // set the font HSSFFont headfont = wb. createFont (); headfont. setFontName (""); headfont. setFontHeightInPoints (short) 20); // font size headfont. setBoldweight (HSSFFont. BOLDWEIGHT_BOLD); // bold HSSFFont font1 = wb. createFont (); font1.setFontName (""); font1.setFontHeightInPoints (short) 12); // font size HSSFFont font3 = wb. createFont (); font3.setFontName (""); font3.setFontHeightInPoints (short) 12); font3.setColor (HSSFFont. COLOR_RED); font3.setUnderline (byte) 1); // Header style HSSFCellStyle headstyle = wb. createCellStyle (); headstyle. setFont (headfont); headstyle. setAlignment (HSSFCellStyle. ALIGN_CENTER); // headstyle in the left-right corner. setverticalignment (HSSFCellStyle. VERTICAL_CENTER); // center headstyle. setLocked (true); headstyle. setWrapText (true); // automatically wrap // body style HSSFCellStyle = wb. createCellStyle (); style. setFont (font1); HSSFCellStyle style2 = wb. createCellStyle (); style2.setFont (font1); style2.setAlignment (HSSFCellStyle. ALIGN_CENTER); // create a center format style2.setBorderBottom (HSSFCellStyle. BORDER_THIN); // The Bottom Border style2.setBorderLeft (HSSFCellStyle. BORDER_THIN); // The Left Border style2.setBorderRight (HSSFCellStyle. BORDER_THIN); // The Right Border style2.setBorderTop (HSSFCellStyle. BORDER_THIN); // Add row 0th to the table header in step 3. Note that in earlier versions, poi has a limit on the number of rows in Excel, add query condition sheet. setColumnWidth (* 5); sheet. setColumnWidth (* 5); sheet. setColumnWidth (2,1000*5); sheet. setColumnWidth (3,1000*5); sheet. setColumnWidth (4,1000*5); sheet. setColumnWidth (5, * 5); sheet. setColumnWidth (6,1000*5); sheet. setColumnWidth (7,1000*5); HSSFRow row = sheet. createRow (int) 0); // Step 4, create a cell, and set the value header to set the center List of the header <SssbtzdBean> list1 = getSssbtzdById (id ); sssbtzdBean bean = list1.get (0); List <SssbtzdmxBean> list = sssbtzdDao. getSssbtzdmxById (bean. getId (); row. setHeightInPoints (float) 29.25); HSSFCell head = row. createCell (int) 0); head. setCellValue (new HSSFRichTextString ("Device Damage notification (" + bean. getId () + ")"); head. setCellStyle (headstyle);/*** merge cells ** first parameter: number of rows in the first cell (starting from 0) * second parameter: number of rows in the second cell (starting from 0) * third parameter: Number of columns in the first cell (starting from 0) * Fourth parameter: Number of columns in the second cell (starting from 0) */sheet. addMergedRegion (new CellRangeAddress (,); sheet. addMergedRegion (new CellRangeAddress (,); row = sheet. createRow (int) 2); row. setHeightInPoints (float) 19.5); cell = row. createCell (int) 0); string = "line" + bean. getXb () + "notification" + bean. getTzxlcj () + bean. getTzgq (); richtext = new HSSFRichTextString (string); richtext. applyFont (0, string. length (), font1); start = "linear ". length (); end = start + bean. getXb (). length (); richtext. applyFont (start, end, font3); start = end + "notification ". length (); end = start + bean. getTzxlcj (). length () + bean. getTzgq (). length (); richtext. applyFont (start, end, font3); cell. setCellValue (richtext); cell. setCellStyle (style); sheet. addMergedRegion (new CellRangeAddress (2, 2, 0, 7 ));


This is a small example written by myself. Import an HSSF jar to run it.



Package outExcle; import java. io. fileOutputStream; import java. util. list; 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. HSSFRichTextString; 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. ss. usermodel. richTextString; import org. apache. poi. ss. util. cellRangeAddress; public class Outxls {/*** export the device injury Notice * @ param id * @ return */public static String outExcleSssbtzd () {boolean result = false; // step 1, create a webbook, corresponding to an Excel file HSSFWorkbook wb = new HSSFWorkbook (); // step 2, add a sheet to the webbook, corresponding to the sheet hssfsheet = wb in the Excel file. createSheet ("sheet1"); // set the font HSSFFont headfont = wb. createFont (); headfont. setFontName (""); headfont. setFontHeightInPoints (short) 20); // font size headfont. setBoldweight (HSSFFont. BOLDWEIGHT_BOLD); // bold HSSFFont font1 = wb. createFont (); font1.setFontName (""); font1.setFontHeightInPoints (short) 12); // font size HSSFFont font3 = wb. createFont (); font3.setFontName (""); font3.setFontHeightInPoints (short) 12); font3.setColor (HSSFFont. COLOR_RED); font3.setUnderline (byte) 1); // Header style HSSFCellStyle headstyle = wb. createCellStyle (); headstyle. setFont (headfont); headstyle. setAlignment (HSSFCellStyle. ALIGN_CENTER); // headstyle in the left-right corner. setverticalignment (HSSFCellStyle. VERTICAL_CENTER); // center headstyle. setLocked (true); headstyle. setWrapText (true); // automatically wrap // body style HSSFCellStyle = wb. createCellStyle (); style. setFont (font1); HSSFCellStyle style2 = wb. createCellStyle (); style2.setFont (font1); style2.setAlignment (HSSFCellStyle. ALIGN_CENTER); // create a center format style2.setBorderBottom (HSSFCellStyle. BORDER_THIN); // The Bottom Border style2.setBorderLeft (HSSFCellStyle. BORDER_THIN); // The Left Border style2.setBorderRight (HSSFCellStyle. BORDER_THIN); // The Right Border style2.setBorderTop (HSSFCellStyle. BORDER_THIN); // Add row 0th to the table header in step 3. Note that in earlier versions, poi has a limit on the number of rows in Excel, add query condition sheet. setColumnWidth (* 5);, // set the width of sheet. setColumnWidth (* 5); sheet. setColumnWidth (2,1000*5); sheet. setColumnWidth (3,1000*5); sheet. setColumnWidth (4,1000*5); sheet. setColumnWidth (5, * 5); sheet. setColumnWidth (6,1000*5); sheet. setColumnWidth (7,1000*5); HSSFRow row = sheet. createRow (int) 0); // Step 4: Create a cell and set the value header to set the center row of the header. setHeightInPoints (float) 29.25); HSSFCell head = row. createCell (int) 0); head. setCellValue (new HSSFRichTextString ("set underline"); head. setCellStyle (headstyle);/*** merge cells ** first parameter: number of rows in the first cell (starting from 0) * second parameter: number of rows in the second cell (starting from 0) * third parameter: Number of columns in the first cell (starting from 0) * Fourth parameter: Number of columns in the second cell (starting from 0) */sheet. addMergedRegion (new CellRangeAddress (,); row = sheet. createRow (int) 1); row. setHeightInPoints (float) 21); HSSFCell cell = row. createCell (int) 0); String string = "content 1:" + "underline 1" + "content 2:" + "underline 2 "; HSSFRichTextString richtext = new HSSFRichTextString (string); richtext. applyFont (0, string. length (), font1); // set the unified format int start = "content 1 :". length (); int end = start + "underline 1 ". length (); richtext. applyFont (start, end, font3); // set the "underline" Format: start = end + "content 2 :". length (); end = start + "underline 2 ". length (); richtext. applyFont (start, end, font3); cell. setCellValue (richtext); sheet. addMergedRegion (new CellRangeAddress (,); // merge cells. // save the file to the specified position. try {String path; path = ("C: /temp/damage device notification .xls "); FileOutputStream fout = new FileOutputStream (path); wb. write (fout); fout. close (); result = true;} catch (Exception e) {e. printStackTrace ();} if (result) return "successful"; return "failed";} public static void main (String [] args) {System. out. println ("export C:/temp/damage device notification .xls ------" + outExcleSssbtzd ());}}
Note:

1. My example is placed on the Baidu network disk, and the jar is also in it... Http://pan.baidu.com/s/1IpXS


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.