public class JxlOpExcel { |
public static void main(String[] args) { |
try { |
// 创建可写入的 Excel工作薄 |
WritableWorkbook wb = Workbook.createWorkbook(new File("c:/test.xls")); |
// 创建Excel工作表 |
WritableSheet ws = wb.createSheet("sheet1", 0); |
// 添加Label对象 |
Label label1 = new Label(0, 0, "测试创建Excel"); |
ws.addCell(label1); |
// 添加带有字型Formatting的对象 |
WritableFont wf = new WritableFont( |
WritableFont.TIMES, 18, WritableFont.BOLD, true); |
WritableCellFormat wcfF = new WritableCellFormat( |
wf); |
Label labelCF = new Label(1, 0, |
"This is a Label Cell", wcfF); |
ws.addCell(labelCF); |
// 添加带有字体颜色Formatting的对象 |
WritableFont wfc = new WritableFont( |
WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, |
UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.RED); |
WritableCellFormat wcfFC = new WritableCellFormat( |
wfc); |
Label labelCFC = new Label(1, 0, |
"This is a Label Cell", wcfFC); |
ws.addCell(labelCFC); |
// 添加Number对象 |
jxl.write.Number labelN = new jxl.write.Number(0, 1, 3.1415926); |
ws.addCell(labelN); |
// 添加带有formatting的 Number对象 |
NumberFormat nf = new NumberFormat("#.##"); |
WritableCellFormat wcfN = new WritableCellFormat( |
nf); |
jxl.write.Number labelNF = new jxl.write.Number(1, 1, 3.1415926, |
wcfN); |
ws.addCell(labelNF); |
// 添加Boolean对象 |
jxl.write.Boolean labelB = new jxl.write.Boolean(0, 2, false); |
ws.addCell(labelB); |
// 添加DateTime对象 |
DateTime labelDT = new DateTime(0, 3, |
new java.util.Date()); |
ws.addCell(labelDT); |
// 添加带有formatting的DateFormat对象 |
DateFormat df = new DateFormat( |
"dd MM yyyy hh:mm:ss"); |
WritableCellFormat wcfDF = new WritableCellFormat( |
df); |
DateTime labelDTF = new DateTime(1, 3, |
new java.util.Date(), wcfDF); |
ws.addCell(labelDTF); |
wb.write(); // 写入Exel工作表 |
wb.close(); // 关闭Excel工作薄对象 |
} catch (IOException e) { |
e.printStackTrace(); |
} catch (RowsExceededException e) { |
e.printStackTrace(); |
} catch (WriteException e) { |
e.printStackTrace(); |
} |
} |
} |
jxl.jar (708.7 KB) |
jxl 创建EXCEL
最新推荐文章于 2021-10-14 21:32:27 发布