excel 写入

package excel;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;

/**
 * 生成excel文件
 * Created by Lenovo on 2017/11/22.
 */
public class WriteExcel {
    private String errorInfo;
    public boolean writer(String filePath,List<List<String>> data){
        File file = new File(filePath);
        //创建工作文档对象
        Workbook wb=null;
        if(WDWUtil.isExcel2007(filePath)){
            wb = new XSSFWorkbook();
        }else if(WDWUtil.isExcel2003(filePath)){
            wb = new HSSFWorkbook();
        }else{
            errorInfo="文件格式不正确";
            return false;
        }
        //创建sheet对象
        Sheet sheet = wb.createSheet("sheet1");
           //添加表头
            Row row = sheet.createRow(0);
            Cell cell = row.createCell(0);
            cell.setCellValue("测试数据表单详情");

            row.setHeight((short)540);
            CellStyle cellStyle = wb.createCellStyle();
            cellStyle.setFillForegroundColor(HSSFColor.PALE_BLUE.index);//单元格背景颜色
            cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);//以前景色方式填充
            cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);//垂直
            cellStyle.setAlignment(CellStyle.ALIGN_CENTER);//水平
            cellStyle.setWrapText(true);//单元格显示不下时自动换行

            cell.setCellStyle(cellStyle);//给表头添加样式(样式  居中)

            Font font = wb.createFont();
            font.setBoldweight(Font.BOLDWEIGHT_BOLD);
            font.setFontName("宋体");
            font.setFontHeight((short)280);
            cellStyle.setFont(font);
            //单元格合并(起始行,起始列,结束行,结束列)
            sheet.addMergedRegion(new CellRangeAddress(0,0,0,7));
            sheet.autoSizeColumn(5200);//自动调整列宽

            //循环写入数据
            Row row1;
            Cell cell1;
            for(int i=0;i<data.size();i++){
                row1 = sheet.createRow(i + 1);
                for(int c=0;c<data.get(i).size();c++){
                    cell1 = row1.createCell(c);
                    cell1.setCellValue(data.get(i).get(c));
                    //设置第一行样式
                    if(i == 0){
                        cell1.setCellStyle(cellStyle);
                        sheet.setColumnWidth(c, 10 * 256);//设置第二行的列宽
                        row1.setHeight((short) 540);
                    }
                }
            }
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(filePath);
                wb.write(fos);
                fos.flush();
                fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        return true;
    }
}
class  WDWUtil{
    public static boolean isExcel2003(String filePath){
        return filePath.matches("^.+\\.(?i)(xls)$");
    }
    public static boolean isExcel2007(String filePath){
        return filePath.matches("^.+\\.(?i)(xlsx)$");
    }

}
package excel;

import java.util.List;

/**
 * Created by Lenovo on 2017/11/22.
 */
public class ExcelTest {
    public static void main(String[] args) {
        ImportExcel poi = new ImportExcel();
        List<List<String>> read = poi.read("C:\\Users\\Lenovo\\Desktop\\1.xlsx");
        /*for(int i=0;i<read.size();i++){
            for (int j=0;j<read.get(i).size();j++){
                System.out.println("第"+i+"行,第"+j+"列:"+read.get(i).get(j));
            }
        }*/
        WriteExcel writeExcel = new WriteExcel();
        writeExcel.writer("C:\\Users\\Lenovo\\Desktop\\2.xlsx",read);

    }
}

 

posted @ 2017-11-22 14:48  zmoony  阅读(119)  评论(0)    收藏  举报