乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      POI設(shè)置Excel的格式 字體樣式 美化

       荒野上的狼 2011-01-07
      POI設(shè)置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.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.hssf.util.HSSFColor;

      /**
      *
      *
      @author hadeslee
      */
      public class Test2{
          
          
      /** Creates a new instance of Test2 */
          
      public Test2() {
           }
          
      public static void main(String[] args)throws Exception {
              
      //聲明一個工作薄
               HSSFWorkbook wb=new HSSFWorkbook();
              
      //生成一個表格
               HSSFSheet sheet=wb.createSheet("表格1");
              
      //生成一個列
               HSSFRow row=sheet.createRow(0);
              
      //生成一個樣式
               HSSFCellStyle style=wb.createCellStyle();
              
      //設(shè)置這些樣式
               style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
               style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
               style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
               style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
               style.setBorderRight(HSSFCellStyle.BORDER_THIN);
               style.setBorderTop(HSSFCellStyle.BORDER_THIN);
               style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
              
      //生成一個字體
               HSSFFont font=wb.createFont();
               font.setColor(HSSFColor.VIOLET.index);
               font.setFontHeightInPoints((
      short)16);
               font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
              
      //把字體應(yīng)用到當(dāng)前的樣式
               style.setFont(font);
              
      //填充單元格
              for(short i=0;i<5;i++){
                  
      //聲明一個單元格
                   HSSFCell cell=row.createCell(i);
                  
      //設(shè)置單元格的字符值
                   cell.setCellValue(new HSSFRichTextString(""+i+""));
                  
      //設(shè)置單元格的樣式
                   cell.setCellStyle(style);
               }
               FileOutputStream fout
      =new FileOutputStream("我的第一個EXCEL.xls");
              
      //輸出到文件
               wb.write(fout);
               fout.close();
           }
      }

       

      public static void main(String[] args) {
              
      try {
                   HSSFWorkbook wb
      = new HSSFWorkbook();
                   HSSFSheet sheet
      = wb.createSheet();
                   HSSFRow row
      = sheet.createRow(0);
                   row.setHeight((
      short) 25);//目的是想把行高設(shè)置成25px
                   FileOutputStream fileOut = new FileOutputStream("c:\\a.xls");
                   wb.write(fileOut);
                   fileOut.close();
               }

              
      catch (Exception e) {
                   e.printStackTrace();
               }

           }

      打開a.xls發(fā)現(xiàn)結(jié)果不是我想要的,第一行的高度都沒有,沒有報錯說明代碼有問題,為什么回沒有高度呢?是不是單位不一樣呢?我把row.setHeight((short) 25);改成了row.setHeight((short) 250);結(jié)果發(fā)現(xiàn)第一行出來了,但是這是怎么一個換算關(guān)系呢?我查看了一下導(dǎo)出的Excel第一行高是16像素,換算一下得出row.setHeight((short) 15.625);表示行高為一個像素,那么想設(shè)成幾個像素就好做了。比如
      row.setHeight((short) (15.625*n));//n為行高的像素數(shù)。
      其實在API中還有一個HSSFRow 對象還有一個設(shè)置行高的函數(shù)setHeightInPoints(float height);這個函數(shù)中參數(shù)就是行高的像素數(shù),比setHeight函數(shù)要方便多了。
      行高設(shè)置完成了,接下來設(shè)置列寬

          public static void main(String[] args) {
              
      try {
                   HSSFWorkbook wb
      = new HSSFWorkbook();
                   HSSFSheet sheet
      = wb.createSheet();
                   HSSFRow row
      = sheet.createRow(0);
                   row.setHeight((
      short) 250);
                   sheet.setColumnWidth((
      short) 0, (short) 250);
                   FileOutputStream fileOut
      = new FileOutputStream("c:\\a.xls");
                   wb.write(fileOut);
                   fileOut.close();
               }

              
      catch (Exception e) {
                   e.printStackTrace();
               }

           }

      接下來說說sheet.setColumnWidth((short) 0, (short) 250);
      第一個參數(shù)表示要為第幾列設(shè)置,第二個參數(shù)表示列的寬度,看看上面的代碼按說第一行第一列的單元格形狀應(yīng)該是個正方形,因為寬和高都是250,但是打開導(dǎo)出后的Excel發(fā)現(xiàn)寬度沒有高度大,是個長方形,查看該列的寬度僅為7個像素,看來行高和列寬的單位是不一樣的,同樣換一算sheet.setColumnWidth((short) 0, (short) (35.7));表示高度為一個像素,同樣設(shè)置列寬的像素為sheet.setColumnWidth((short) 0, (short) (35.7*n));//n為列高的像素數(shù)。

       

      public class MergedCells {
           public static void main(String[] args) throws IOException {
               HSSFWorkbook wb = new HSSFWorkbook();
               HSSFSheet sheet = wb.createSheet("new sheet");

               HSSFRow row = sheet.createRow((short) 1);
               HSSFCell cell = row.createCell((short) 1);
               cell.setCellValue("This is a test of merging");

               sheet.addMergedRegion(new Region(1, (short) 1, 1, (short) 2));

               // Write the output to a file
               FileOutputStream fileOut = new FileOutputStream("workbook.xls");
               wb.write(fileOut);
               fileOut.close();

           }
      }


      public class WorkingWithFonts {
           public static void main(String[] args) throws IOException {
               HSSFWorkbook wb = new HSSFWorkbook();
               HSSFSheet sheet = wb.createSheet("new sheet");

               // Create a row and put some cells in it. Rows are 0 based.
               HSSFRow row = sheet.createRow((short) 1);

               // Create a new font and alter it.
               HSSFFont font = wb.createFont();
               font.setFontHeightInPoints((short) 24);
               font.setFontName("Courier New");
               font.setItalic(true);
               font.setStrikeout(true);

               // Fonts are set into a style so create a new one to use.
               HSSFCellStyle style = wb.createCellStyle();
               style.setFont(font);

               // Create a cell and put a value in it.
               HSSFCell cell = row.createCell((short) 1);
               cell.setCellValue("This is a test of fonts");
               cell.setCellStyle(style);

               // Write the output to a file
               FileOutputStream fileOut = new FileOutputStream("workbook.xls");
               wb.write(fileOut);
               fileOut.close();

           }
      }



      HSSFRow row = sheet2.createRow(2);
               row.setHeightInPoints(240);
               sheet2.setColumnWidth((short) 2, (short) 9000);

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多