Следующий код работает для создания файла Excel, для написания листа с использованием Java Excel API:
import jxl.*;
import jxl.write.*;
import java.io.*;
public class CreateExcel_JxlApi {
public static void main(String[] args) {
//create WorkbookSettings object
WorkbookSettings ws = new WorkbookSettings();
try{
//create work book
//WritableWorkbook workbook = Workbook.createWorkbook(new File("F:/Tips/JExcelTip/TestReport.xls"), ws);
WritableWorkbook workbook = Workbook.createWorkbook(new File("F:\\TestReport.xls"), ws);
System.out.println("Did excel file create?");
//create work sheet
WritableSheet workSheet = null;
workSheet = workbook.createSheet("Test Report" ,0);
SheetSettings sh = workSheet.getSettings();
//Creating Writable font to be used in the report
WritableFont normalFont = new WritableFont(WritableFont.createFont("MS Sans Serif"),
WritableFont.DEFAULT_POINT_SIZE,
WritableFont.NO_BOLD, false);
//creating plain format to write data in excel sheet
WritableCellFormat normalFormat = new WritableCellFormat(normalFont);
normalFormat.setWrap(true);
normalFormat.setAlignment(jxl.format.Alignment.CENTRE);
normalFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
normalFormat.setWrap(true);
normalFormat.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN,
jxl.format.Colour.BLACK);
//write to datasheet
workSheet.addCell(new jxl.write.Label(0,0,"User Name",normalFormat));
workSheet.addCell(new jxl.write.Label(1,0,"Password",normalFormat));
//write to the excel sheet
workbook.write();
//close the workbook
workbook.close();
}catch(Exception e){
e.printStackTrace();
}
}
}