国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

通過jxl.jar 讀取、導(dǎo)出excel的實(shí)例代碼

2019-11-26 16:14:44
字體:
供稿:網(wǎng)友

復(fù)制代碼 代碼如下:

 package export.excel;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import export.vo.PersonVo;

/**導(dǎo)出excel文件
 *
 * @author 路人甲
 *
 */
public class ExportExcel {

 
 public List<PersonVo> personVoList;

 public ExportExcel()
 {
  initdata();
 }

 /**
  * 初始化數(shù)據(jù)
  */
 public void initdata()
 {
  // 隨機(jī)函數(shù)
  Random random = new Random();
  personVoList = new ArrayList<PersonVo>();
  // 初始化100個(gè)人進(jìn)集合
  PersonVo personVo = null;
  for (int i = 0; i < 100; i++)
  {
   personVo = new PersonVo();
   personVo.setId("" + i);
   personVo.setName("路人" + i);
   if (i%2==0)
   {
    personVo.setSex("男");
   }
   else
   {
    personVo.setSex("女");
   }
   // 取100歲以內(nèi)的隨機(jī)年齡
   personVo.setAge("" + random.nextInt(100));
   personVoList.add(personVo);
  }
 }

 /**導(dǎo)出數(shù)據(jù)以Excel格式導(dǎo)出
  * exportName 導(dǎo)出文件名稱
  * @param exportName
  */
 public void exportPerson(String exportName)
 {
  try{
   File excelFile = new File(exportName + "Temp.xls");
   // 文件格式
   WritableCellFormat format = new WritableCellFormat();
   // x  靠左
   format.setAlignment(Alignment.RIGHT);
   // y 靠頂
   format.setVerticalAlignment(VerticalAlignment.TOP);

   // 創(chuàng)建一個(gè)工作文件
   WritableWorkbook writableWorkbook = Workbook.createWorkbook(excelFile);
   // 創(chuàng)建一個(gè)工作簿
   WritableSheet sheet1 = writableWorkbook.createSheet("個(gè)人信息列表", 0);
   //創(chuàng)建行數(shù) 設(shè)置行的寬度
   sheet1.setColumnView(0, 10);
   sheet1.setColumnView(1, 10);
   sheet1.setColumnView(2, 10);
   sheet1.setColumnView(3, 10);
   // 設(shè)置行的值
   sheet1.addCell(new Label(0, 0, "ID", format));
   sheet1.addCell(new Label(1, 0, "姓名", format));
   sheet1.addCell(new Label(2, 0, "性別", format));
   sheet1.addCell(new Label(3, 0, "年齡", format));

   // 把集合寫入到excel中
   int rowNum = 1;
   for (PersonVo bean : personVoList)
   {
    int colspanNum = 0;
    sheet1.addCell(new Label(colspanNum, rowNum, bean.getId(), format));
    sheet1.addCell(new Label(colspanNum++, rowNum, bean.getName(), format));
    sheet1.addCell(new Label(colspanNum++, rowNum, bean.getSex(), format));
    sheet1.addCell(new Label(colspanNum++, rowNum, bean.getAge(), format));

    rowNum++;
   }

   writableWorkbook.write();
   writableWorkbook.close();
//   如果是在網(wǎng)絡(luò)下載的,那么就寫這些
//            super.getHttpServletResponse().setContentType("application/x-msdownload");
//            String encodetittle = new String(excelName.getBytes("GBK"), "ISO-8859-1");
//            super.getHttpServletResponse().addHeader("Content-Disposition","attachment;filename="+encodetittle+".xls");
            FileInputStream finput = new FileInputStream(excelFile);
//            OutputStream output = super.getHttpServletResponse().getOutputStream();
            File fout = new File(exportName + ".xls");
            OutputStream output = new FileOutputStream(fout);
            BufferedInputStream buffin = new BufferedInputStream(finput);
            BufferedOutputStream buffout = new BufferedOutputStream(output);
            byte[] buffer = new byte[4096];
            int count = 0;
            while ((count = buffin.read(buffer, 0, buffer.length)) > 0) {
                buffout.write(buffer, 0, count);
            }
            buffin.close();
            buffout.close();
            finput.close();
            output.close();
            excelFile.delete();
  }
  catch (Exception e) {
   e.printStackTrace();
  }
  finally
  {
   System.out.println("完成導(dǎo)出操作");
  }

 }

 /**導(dǎo)入數(shù)據(jù)以Excel格式導(dǎo)出
  *
  * @param importExcel 導(dǎo)入Excel文件名稱
  */
 public void importPerson(String importExcel)
 {
  try{
   File excelFile = new File(importExcel+".xls");
   // 創(chuàng)建一個(gè)工作文件
   Workbook workbook = Workbook.getWorkbook(excelFile);
   // 獲得第一個(gè)工作簿 這里有兩種方法獲取sheet表,1為名字,而為下標(biāo),從0開始
   Sheet sheet = workbook.getSheet(0);
   // 總記錄數(shù)
   int allRow = sheet.getRows();
   int allColspan = sheet.getColumns();
   System.out.println(allRow);
   System.out.println(allColspan);
   // 取數(shù)據(jù)
   for (int i=0; i<allRow; i++)
   {
    // 取出每一列的值
    for (int j=0; j<allColspan; j++)
    {
//     sheet.getCell(列數(shù), 行數(shù));
     Cell cell = sheet.getCell(j, i);
     // 打印出該列的值
     System.out.print(cell.getContents() + "/t");
    }
    System.out.println();
   }
   //關(guān)閉
   workbook.close();
  }catch (Exception e) {
   e.printStackTrace();
  }
  finally
  {
   System.out.println("完成導(dǎo)入操作");
  }
 }


 /**
  * @param args
  */
 public static void main(String[] args) {

  ExportExcel exportExcel = new ExportExcel();
  // 設(shè)置路徑
  String srcPath = "C:/Quarantine/PersonVo";
//  導(dǎo)出
//  exportExcel.exportPerson(srcPath);
  // 讀取
  exportExcel.importPerson(srcPath);
 }

}
 

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 东海县| 龙口市| 师宗县| 西畴县| 枣庄市| 嘉定区| 柳河县| 长沙市| 泰宁县| 延川县| 三穗县| 姜堰市| 洪洞县| 江津市| 新乐市| 云和县| 应城市| 贡觉县| 金昌市| 朝阳区| 即墨市| 河源市| 林周县| 阳江市| 万源市| 英德市| 宁晋县| 长垣县| 舒兰市| 贡觉县| 贵溪市| 柯坪县| 大连市| 易门县| 钦州市| 萝北县| 额济纳旗| 台州市| 沁源县| 兰坪| 宁陵县|