完成这个实验,你需要下载jxl
jar包,具体方法自行百度。
接下来我将直接使用具体代码进行讲解我的实现过程。
文件名:ExcelAction.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
package net.kuangmeng.excel;
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import com.opensymphony.xwork2.ActionSupport; import jxl.Workbook; import jxl.format.Alignment; import jxl.format.Colour; import jxl.format.UnderlineStyle; import jxl.format.VerticalAlignment; import jxl.write.Label; import jxl.write.WritableCellFormat; import jxl.write.WritableFont; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import jxl.write.WriteException; import jxl.write.biff.RowsExceededException;
@SuppressWarnings("serial") public class ExcelAction extends ActionSupport{ public static void main(String[] args) { list<String> listth = new ArrayList<String>; list<String> listtd = new ArrayList<String>; int num ; exportExcel(tablename,listth,listtd,num); }
public static void exportExcel(String fileName,List<String> listth,List<String> listtd,int num) { String excelName ="C:\\Users\\meng\\Desktop\\"+fileName+".xls"; try { File excelFile = new File(excelName); if (excelFile.exists()) excelFile.delete(); WritableWorkbook book = Workbook.createWorkbook(excelFile); WritableSheet sheet = book.createSheet("Up2U导出表格 ", 0); jxl.write.WritableFont wfc = new jxl.write.WritableFont( WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK); jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat( wfc); jxl.write.WritableCellFormat wcfF = new jxl.write.WritableCellFormat(wfc); wcfF.setBackground(jxl.format.Colour.BLACK); wcfFC.setBackground(jxl.format.Colour.GRAY_25); wcfFC.setAlignment(jxl.format.Alignment.CENTRE);
for(int i=0;i<listth.size()/(num-2);i++){ for(int j=0;j<num-2;j++){ sheet.addCell(new Label(j,i,listth.get(i*(num-2)+j),wcfFC)); } } for(int i=listth.size()/(num-2);i<(listth.size()+listtd.size())/(num-2);i++){ for(int j=0;j<num-2;j++){ sheet.addCell(new Label(j,i,listtd.get((i-1)*(num-2)+j),wcfF)); } } book.write(); book.close(); System.out.println("Excel创建成功"); } catch (Exception e) { System.out.println(e); } } }
|
The link of this page is https://blog.nooa.tech/articles/7725d215/ . Welcome to reproduce it!