正在处理应用程序以从数据库中读取值并将其存储在excel文件中。 现在我需要为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 :(得分:4)
(测试,它的工作原理)
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);
答案 1 :(得分:2)
了解POI API:http://poi.apache.org/apidocs/index.html
Cell有方法setCellStyle(CellStyle style)
。 CellStyle
有多种方法可以处理单元格不同部分的颜色(背景,边框,字体等)。