Я работаю над приложением, чтобы прочитать значения из базы данных и сохранить их в файле 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());
}
}
}