WriteExecl 写入 Excel 文档
package cn.jxl; import java.io.File; import jxl.Workbook; import jxl.write.Label; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; /** * 写入一个 Execl 文件 * */ public class WriteExecl { public static void main(String[] args) { try { //1.打开文件 WritableWorkbook book = Workbook.createWorkbook(new File("E:/chaoyi/test.xls")); //2.生成名为“第一页”的工作表,参数 0 表示这是第一页 WritableSheet sheet = book.createSheet("第一页", 0); //3.定义文本内容(列,行)) Label label = new Label(0,0,"这是内容"); //4.将定义好的单元格添加到工作表中 sheet.addCell(label); //5.定义数值类型,必须使用 Number 的完整包路径,否则有语法歧义 jxl.write.Number number = new jxl.write.Number(1,0,5555.123456); sheet.addCell(number); //6.写入数据并关闭文件 book.write(); book.close(); System.out.println("写入成功!"); } catch (Exception e) { e.printStackTrace(); System.out.println("写入失败!"); } } }
ReadExecl 读取 Excel 文档
package cn.jxl; import java.io.File; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; /** * 读取 Execl 文件 * */ public class ReadExecl { public static void main(String[] args) { String path = "E:/chaoyi/test.xls"; try { //1.创建读取的文件对象 Workbook book = Workbook.getWorkbook(new File(path)); //2.得到 Sheet,参数“名字” Sheet sheet = book.getSheet("第一页"); //3.得到单元格的对象,(列,行) Cell cell = sheet.getCell(0, 0); //4.得到内容,以字符串返回 String content = cell.getContents(); System.out.println(content); //5.关闭工作薄 System.out.println("写入成功!"); book.close(); } catch (Exception e) { e.printStackTrace(); System.out.println("写入失败!"); } } }
WriteDemoExecl 写入 Excel 文档2
package cn.jxl; import java.io.FileOutputStream; import java.io.OutputStream; import jxl.Workbook; import jxl.write.WritableFont; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; /** * 写入 Execl 文件 * */ public class WriteDemoExecl { public static void main(String[] args) { //输出的 execl 文件名 String targetfile = "E:/chaoyi/demo.xls"; //输出的 execl 文件工作表名 String worksheet = "列表"; //execl 工作表的标题 String[] title = {"编号","姓名","备注"}; WritableWorkbook workbook; try { //通过输出流方式的建立 OutputStream os = new FileOutputStream(targetfile); workbook= Workbook.createWorkbook(os); //添加第一个工作表 WritableSheet sheet = workbook.createSheet(worksheet, 0); //要写入的内容 jxl.write.Label label = null; //写入 title 数组 for(int i = 0; i<title.length;i++){ //Lable (列号,行号,内容) label = new jxl.write.Label(i,0,title[i]); sheet.addCell(label); } //添加数字的 3.14159 到 D5 ,数字默认右对齐 jxl.write.Number number = new jxl.write.Number(3,4,3.14159); sheet.addCell(number); //添加带有字型 Formatting 的对象(字体,大小,加粗,倾斜) jxl.write.WritableFont wf = new jxl.write.WritableFont(WritableFont.TIMES,12, WritableFont.BOLD,true); jxl.write.WritableCellFormat wcfF = new jxl.write.WritableCellFormat(wf); jxl.write.Label labelCF = new jxl.write.Label(4,4,"Hello World",wcfF); sheet.addCell(labelCF); //添加带有字体颜色,带背景颜色 Formatting 的对象(字体,大小,加粗,倾斜,下划线) jxl.write.WritableFont wfc = new jxl.write.WritableFont(WritableFont.ARIAL,11, WritableFont.BOLD,false,jxl.format.UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.RED); jxl.write.WritableCellFormat wcfFc = new jxl.write.WritableCellFormat(wfc); //背景色 wcfFc.setBackground(jxl.format.Colour.YELLOW2); jxl.write.Label labelCFC = new jxl.write.Label(1,5,"Color Wolf",wcfFc); sheet.addCell(labelCFC); //添加带有格式的 Number 对象,保留 2 位小数 jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#.##"); jxl.write.WritableCellFormat wcfN = new jxl.write.WritableCellFormat(nf); jxl.write.Number labelNF = new jxl.write.Number(1,1,3.141234698798,wcfN); sheet.addCell(labelNF); //添加 Boolean 对象 jxl.write.Boolean labelB = new jxl.write.Boolean(0, 2, false); sheet.addCell(labelB); //添加日期和时间对象 jxl.write.DateTime lableDT = new jxl.write.DateTime(0, 3, new java.util.Date()); sheet.addCell(lableDT); //添加带有 formatting 的 DateFormat 对象 jxl.write.DateFormat df = new jxl.write.DateFormat("yyyy年MM月dd日 hh:mm:ss"); jxl.write.WritableCellFormat wcfDF = new jxl.write.WritableCellFormat(df); jxl.write.DateTime labelDTF = new jxl.write.DateTime(1,3,new java.util.Date(),wcfDF); sheet.addCell(labelDTF); //合并单元格(左上角和右下角)sheet.mergeCells(int col1, int row1, int col2, int row2) sheet.mergeCells(4, 5, 8, 10);//左上角和右下角 wfc = new jxl.write.WritableFont(WritableFont.ARIAL,40,WritableFont.BOLD,false, jxl.format.UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.GREEN); jxl.write.WritableCellFormat wchB = new jxl.write.WritableCellFormat(wfc); //居中 wchB.setAlignment(jxl.format.Alignment.CENTRE); labelCFC = new jxl.write.Label(4, 5, "I am bug", wchB); sheet.addCell(labelCFC); //设置边框 jxl.write.WritableCellFormat wcsB = new jxl.write.WritableCellFormat(); wcsB.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THICK); labelCFC = new jxl.write.Label(0, 6, "边框设置", wcsB); sheet.addCell(labelCFC); //关闭 workbook.write(); workbook.close(); System.out.println("写入完成"); } catch (Exception e) { e.printStackTrace(); System.out.println("写入失败"); } } }