import java.io.FileOutputStream; import java.sql.Connection; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; 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.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; public class Expexcel {Private Final Static SimpleDateFormat Format = New SimpleDateFormat ("YYYY-MM-DD HH: MM: SS"); public static void main (string [] args) {system.out.println ("Start Time:" Format. Format (new date ()); genericexcel (true); system.out.println ("end time:" format.format (new date ()));} / ** * @Param HaveTitle * Did you export your head * / public static void genericExcel (Boolean HaveTitle) {Try {class.forname ("com.microsoft.jdbc .sqlserver.SQLServerDriver ".) newInstance (); String url =" jdbc: microsoft: sqlserver: // localhost: 1433; DatabaseName = User4Every "; String user =" sa "; String password =" sa "; Connection conn = java .sql.DriverManager.getConnection (url, user, password); String fileName = "E: //report.xls"; Statement stmt = conn.createStatement (); ResultSet rs = stmt.executeQuery ( "select * from TPJBjbxx") ; ResultSetMetaData rsmd = rs.getMetaData (); int columnCount = rsmd.getColumnCount (); FileOutputStream fileOut = new FileOutputStream (fileName); HSSFWorkbook wb = new HSSFWorkbook (); HSSFSheet sheet = wb.createSheet (0 "");
Wb.setsheetname (0, "Report 1", (Short); hssfcellstyle cs = wb.createcellStyle (); // Format object hssffont fcol = wb.createFont (); // font object, head fcol.setBoldWeight ( HSSFFont.BOLDWEIGHT_BOLD); HSSFRow row = null; HSSFCell cell = null; int nrow = 0; String s_colType; if (haveTitle) {row = sheet.createRow ((short) nrow); for (int i = 0; i