2011-11-17 18 views
2

我正在從應用程序讀取數據庫中的值並將其存儲在Excel文件中。 現在我需要添加不同的顏色到Excel表格中的不同列和行。如何爲java中的excel文件中的列和行添加顏色?

請幫我這個。

在此先感謝。

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package excelproject; 

import java.io.FileOutputStream; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.Statement; 
import java.sql.Timestamp; 
import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Calendar; 
import java.util.Date; 
import java.util.GregorianCalendar; 
import org.apache.poi.hssf.usermodel.HSSFCell; 
import org.apache.poi.hssf.usermodel.HSSFCellStyle; 
import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
import org.apache.poi.hssf.usermodel.HSSFRow; 
import org.apache.poi.hssf.usermodel.HSSFSheet; 
import org.apache.poi.hssf.util.HSSFColor; 

/** 
* 
* @author rajagopal 
*/ 
public class ExcelProject { 

    private static Connection con; 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     // TODO code application logic here 
     try { 
      Class.forName("com.mysql.jdbc.Driver").newInstance(); 
      Connection connection = DriverManager.getConnection(
        "jdbc:mysql://localhost:3306/excelproject", "root", "root"); 
      Statement st = connection.createStatement(); 
      ResultSet rs = st.executeQuery("select * from empdetails"); 
      HSSFWorkbook wb = new HSSFWorkbook(); 
      HSSFCellStyle style = wb.createCellStyle(); 

      ArrayList lis = new ArrayList(); 
      DateFormat format = new SimpleDateFormat("yyyy-MM-dd_hh.mm.ss"); 
      String timeStamp = format.format(new Date()); 
      String Excel = "Employee_Records_" + timeStamp; 
      HSSFSheet sheet = wb.createSheet(Excel); 
      HSSFRow rowhead = sheet.createRow((short) 0); 






      rowhead.createCell((short) 0).setCellValue("Serial Number"); 
      rowhead.createCell((short) 1).setCellValue("Employee Name"); 
      rowhead.createCell((short) 2).setCellValue("Address"); 
      rowhead.createCell((short) 3).setCellValue("Contact Number"); 
      rowhead.createCell((short) 4).setCellValue("Email"); 
      int i = 1; 
      while (rs.next()) { 
       HSSFRow row = sheet.createRow((short) i); 

       row.createCell((short) 0).setCellValue(Integer.toString(rs.getInt("id"))); 
       row.createCell((short) 1).setCellValue(rs.getString(2)); 
       row.createCell((short) 2).setCellValue(rs.getString(3)); 
       row.createCell((short) 3).setCellValue(Integer.toString(rs.getInt(4))); 
       row.createCell((short) 4).setCellValue(rs.getString(5)); 
       lis.add(rs.getString(2)); 
       System.out.println(rs.getString(2)); 
       i++; 
      } 
      for (int j = 0; j <= lis.size() - 1; j++) { 
       ResultSet rs1 = st.executeQuery("select * from empdetails where name ='" + lis.get(j) + "'"); 
       while (rs1.next()) { 
        HSSFSheet sheet1 = wb.createSheet(rs1.getString(2)); 
        HSSFRow rowhead1 = sheet1.createRow((short) 0); 
        rowhead1.createCell((short) 0).setCellValue("Serial Number"); 
        rowhead1.createCell((short) 1).setCellValue("Employee Name"); 
        rowhead1.createCell((short) 2).setCellValue("Address"); 
        rowhead1.createCell((short) 3).setCellValue("Contact Number"); 
        rowhead1.createCell((short) 4).setCellValue("Email"); 
        HSSFRow row1 = sheet1.createRow((short) 1); 
        row1.createCell((short) 0).setCellValue(Integer.toString(rs1.getInt("id"))); 
        row1.createCell((short) 1).setCellValue(rs1.getString(2)); 
        row1.createCell((short) 2).setCellValue(rs1.getString(3)); 
        row1.createCell((short) 3).setCellValue(Integer.toString(rs1.getInt(4))); 
        row1.createCell((short) 4).setCellValue(rs1.getString(5)); 
       } 
      } 

      String filename = "c:\\ExcelFile_" + timeStamp +".xls"; 
      FileOutputStream fileOut = new FileOutputStream(filename); 
      wb.write(fileOut); 
      fileOut.close(); 
      System.out.println("Data is saved in excel file."); 
      st.close(); 
     } catch (Exception e) { 
      System.out.println(e.getMessage()); 
     } 
    } 


} 
+0

如何在您的數據寫入到excel文件?你在使用POI嗎? – AlexR

+0

是的,我正在使用poi。 –

回答

2

就以POI API來看看:http://poi.apache.org/apidocs/index.html

細胞有方法setCellStyle(CellStyle style)CellStyle有多種方法處理單元格的不同部分(背景,邊框,字體等)的顏色。

+1

您還必須設置填充模式。 – zeller

+0

沒錯。有很多可能性。 – AlexR

4

http://poi.apache.org/spreadsheet/quick-guide.html#CustomColors (經測試,它的工作原理)

HSSFWorkbook wb = new HSSFWorkbook(); 
HSSFSheet sheet = wb.createSheet(); 
HSSFRow row = sheet.createRow((short) 0); 
HSSFCell cell = row.createCell((short) 0); 
cell.setCellValue("Default Palette"); 

//apply some colors from the standard palette, 
// as in the previous examples. 
//we'll use red text on a lime background 

HSSFCellStyle style = wb.createCellStyle(); 
style.setFillForegroundColor(HSSFColor.LIME.index); 
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); 

HSSFFont font = wb.createFont(); 
font.setColor(HSSFColor.RED.index); 
style.setFont(font); 

cell.setCellStyle(style); 
+0

嗨,人們,我嘗試了所有上述方法沒有任何工作爲我。任何人都可以幫助我。 –

+0

正如我上面提到的,我測試了代碼,它正在工作。也許如果你可以與我們分享你的代碼,我們可以進一步幫助。 – ruhsuzbaykus

+0

您試圖獲得對單元格的引用並相應地設置其樣式?因爲在你的代碼中沒有對單元格的引用。 – ruhsuzbaykus

相關問題