如何通过jxl向excel中追加数据
RT,每执行一次程序,就在excel现有数据下面行开始存数据。。。
package testtool;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.json.JSONException;
import jxl.Cell;
import jxl.CellType;
import jxl.DateCell;
import jxl.NumberCell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
/*
* 解析excel,并且封装为bean,存入数据库
*/
public class WriteToExcel {
// list存放数据, filename是excel存放的路径以及名称
public static void ExportExcel(List<ExcelParameterBean> list) throws IOException {
WritableWorkbook book = Workbook.createWorkbook(new File(
"F:\\write.xls"));
try {
// WritableWorkbook book = Workbook.createWorkbook(new File(
// "F:\\write.xls"));
WritableSheet sheet = book.createSheet("sheet1", 0);
// 添加表头, 第一个参数是列,第二个是行
Label label1 = new Label(0, 0, "id"); // 第一列第一行
Label label2 = new Label(1, 0, "comand"); // 第二列第一行
Label label3 = new Label(2, 0, "request"); // 第三列第一行
Label label4 = new Label(3, 0, "exresponse"); // 第四列第一行
Label label5 = new Label(4, 0, "response"); // 第五列第一行
Label label6 = new Label(5, 0, "checkResult"); // 第六列第一行
// 添加到sheet1中
sheet.addCell(label1);
sheet.addCell(label2);
sheet.addCell(label3);
sheet.addCell(label4);
sheet.addCell(label5);
sheet.addCell(label6);
if (list.size() != 0) {
Iterator it = list.iterator();
int i = 1; // 表示行
while (it != null && it.hasNext()) {
ExcelParameterBean stu = (ExcelParameterBean) it.next(); // list里存放学生
Label l1 = new Label(0, i, stu.getId());
Label l2 = new Label(1, i, stu.getCommand());
Label l3 = new Label(2, i, stu.getRequest());
Label l4 = new Label(3, i, stu.getExresponse());
Label l5 = new Label(4, i, stu.getResponse());
Label l6 = new Label(5, i, stu.getCheckResult());
sheet.addCell(l1);
sheet.addCell(l2);
sheet.addCell(l3);
sheet.addCell(l4);
sheet.addCell(l5);
sheet.addCell(l6);
i++;
}
for (int j = 0; j < list.size(); j++) {
ExcelParameterBean u = (ExcelParameterBean) list.get(j);
System.out.println("id=" + u.getId() + " " + "command="
+ u.getCommand() + " " + "request=" + u.getRequest()
+ " " + "exresponse=" + u.getExresponse() + " "
+ "response=" + u.getResponse() + " "
+ "checkResult=" + u.getCheckResult()
// + "qingqiucishu=" + u.getQingqiucishu()+ "bingfashu=" +
// u.getBingfashu()
);}
book.write();
book.close();
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public static void duoxiancheng() throws IOException, JSONException {
// public static void main(String[] args) throws IOException,
// JSONException {
Read.shao();
List<ExcelParameterBean> list = new Batch().doBatch(Read.list);
ExportExcel(list);
}
}
以上是小弟的代码,每次执行都重写个excel,不符合要求
[解决办法]
首先按住ALT+PRSCRN 把当前的界面截图。然后按住WIN+R输入MSPAINT之后CTRL+V将截图进行红绿修改即可
望采纳