我在向新创建的.xlsx文件添加标头时遇到问题 我想看到第一个header ='UserName'和第二个header ='Password'。任何人都可以帮我解决这个问题吗?
XSSFWorkbook new_workbook = new XSSFWorkbook(); // create a blank workbook object
XSSFSheet sheet = new_workbook.createSheet("EMP_DETAILS"); // create a worksheet with caption score_details
// Define the SQL query
String sql = "SELECT login, password FROM \"Permissions\"";
ResultSet rs = stmt.executeQuery(sql);
// Create Map for Excel Data
Map<String, Object[]> excel_data = new HashMap<String, Object[]>();
int row_counter = 0;
//Extract data from result set
while(rs.next()){
row_counter = row_counter+1;
String login = rs.getString("login");
String password = rs.getString("password");
excel_data.put(Integer.toString(row_counter), new Object[] {login, password});
}
rs.close();
// Load data into logical worksheet
Set<String> keyset = excel_data.keySet();
int rownum = 0;
for(String key : keyset){ // loop through the data and add them to the cell
Row row = sheet.createRow(rownum++);
Object [] objArr = excel_data.get(key);
int cellnum = 0;
for(Object obj : objArr){
Cell cell = row.createCell(cellnum++);
if(obj instanceof Double)
cell.setCellValue((Double)obj);
else
cell.setCellValue((String)obj);
}
}
FileOutputStream output_file = new FileOutputStream(new File("File.xlsx")); // create XLSX file
new_workbook.write(output_file); // write excel document to output stream
output_file.close(); // close the file
答案 0 :(得分:0)
我的0.02美元......不要为这么简单的东西创建Excel文件。而是创建一个CSV文件。你写的第一行是
UserName, Password
标题和之后的行只是您用逗号分隔的每列的数据。写入文件后,您可以轻松地在Excel中打开它并将其另存为Excel文件。
此方法比尝试实际编写Excel文件更简单,更容易。
如果你不想这样做,你只需要在开始循环keyset
之前创建一行,就像在任何其他行中一样,并为标题写字符串。
答案 1 :(得分:0)
您可以在此行org.xml.sax.SAXParseException: Attribute name "controls" associated with an element type "audio" must be followed by the ' = ' character.
之后使用以下代码:
ResultSetMetaData rsmd = rs.getMetaData();
然后获取列标题;使用以下:
ResultSet rs = stmt.executeQuery(sql);