I want to create an Excel file and write data just like writing a text file with Java. I tried to change file extension from .txt
to .xls
. But I want to bold letters in the Excel file. How can I do that?
I have tried using the JXL API, but every time I have to create a label I want add no label. Can't O edit row and column of the table?
//Find jar from here "http://poi.apache.org/download.html"
import java.io.*;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFRow;
public class CreateExlFile{
public static void main(String[]args) {
try {
String filename = "C:/NewExcelFile.xls" ;
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("FirstSheet");
HSSFRow rowhead = sheet.createRow((short)0);
rowhead.createCell(0).setCellValue("No.");
rowhead.createCell(1).setCellValue("Name");
rowhead.createCell(2).setCellValue("Address");
rowhead.createCell(3).setCellValue("Email");
HSSFRow row = sheet.createRow((short)1);
row.createCell(0).setCellValue("1");
row.createCell(1).setCellValue("Sankumarsingh");
row.createCell(2).setCellValue("India");
row.createCell(3).setCellValue("sankumarsingh@gmail.com");
FileOutputStream fileOut = new FileOutputStream(filename);
workbook.write(fileOut);
fileOut.close();
workbook.close();
System.out.println("Your excel file has been generated!");
} catch ( Exception ex ) {
System.out.println(ex);
}
}
}
You can use Apache POI for creating native binary xls files.
Or you can use JExcelApi which is another, and somewhat light-weight as far as I can remember, Java library for Excel.
Fair warning about Apache POI's Excel generation... (I know this is an old post, but it's important in case someone looks this up again like I just did)
It had a memory leak issue, which supposedly was solved by 2006, but which people quite recently have still been experiencing. If you want to automate generating a large amount of excel (i.e., if you want to generate a single, large file, a large number of small files, or both), I'd recommend using a different API. Either that, or increasing the JVM stack size to preposterous proportions, and maybe looking into interning strings if you know you won't actually be working with many different strings (although, of course, interning strings means that if you have a large number of different strings, you'll have an entirely different program-crashing memory problem. So, consider that before you go that route).
File fileName = new File(".....\\Fund.xlsx");
public static void createWorkbook(File fileName) throws IOException {
try {
FileOutputStream fos = new FileOutputStream(fileName);
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("fund");
Row row = sheet.createRow(0);
Cell cell0 = row.createCell(0);
cell0.setCellValue("Nav Value");
Cell cell1 = row.createCell(1);
cell1.setCellValue("Amount Change");
Cell cell2 = row.createCell(2);
cell2.setCellValue("Percent Change");
workbook.write(fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Flat files do not allow providing meta information.
I would suggest writing out a HTML table containing the information you need, and let Excel read it instead. You can then use <b> tags to do what you ask for.
I've created an API to create an Excel file more easier.
Create Excel - Creating Excel from Template
Just set the required values upon instantiation then invoke execute(), it will be created based on your desired output directory.
But before you use this, you must have an Excel Template which will be use as a template of the newly created Excel file.
Also, you need Apache POI in your project's class path.
Changing the extension of a file does not in any way change its contents. The extension is just a label.
If you want to work with Excel spreadsheets using Java, read up on the Apache POI library.
To create a spreadsheet and format a cell using POI, see the Working with Fonts example, and use:
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
POI works very well. There are some things you can't do (e.g. create VBA macros), but it'll read/write spreadsheets with macros, so you can create a suitable template sheet, read it and manipulate it with POI, and then write it out.
来源:https://stackoverflow.com/questions/1176080/create-excel-file-in-java