Directly on the bar code
/** * */ package zhongdian.whh.commonclass; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletResponse; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.hssf.util.Region; /**数据导出Excel * 2018/9/10 * @author * */ public class DataToExcel { private DataToExcel(){} /** * 导出Excel * @param response 你懂的 * @param excelFile Excel文件 * @param fileName 导出的文件名 */ public static void export(HttpServletResponse response , HSSFWorkbook excelFile , String fileName) { //写入excel表 response.reset(); response.setContentType( "application/octet-stream;charset=GBK" ); response.setCharacterEncoding("GBK"); response.setHeader( "Content-Disposition", "attachment;filename=" + processExcelFilename(fileName) + ".xls" ); try { excelFile.write(response.getOutputStream( )); } catch (IOException e) { throw new RuntimeException(e); } } /** * 导出Excel * @param response 你懂的 * @param fileName 导出的文件名 * @param columnWidth 列宽 * @param rowName 表头 * @param dataList 数据以List<List<String>>组织 */ public static void export(HttpServletResponse response , String fileName , int columnWidth , String sheetName , String[] rowName, List<List<String>> dataList){ HSSFWorkbook excelFile = null; try { excelFile = toExcel(excelFile, sheetName, columnWidth, rowName, dataList); } catch (IOException e) { ; a RuntimeException the throw new new (E) } export(response, excelFile, fileName); } / ** * stacked converted into Excel files * @param excelFile stack Excel file, the new pass null * @param Table the Table * @param sheetName SheetName * @param the columnWidth column widths after stacking * @return Excel file * @throws IOException * / public static HSSFWorkbook ToExcel (HSSFWorkbook ExcelFile, String sheetName, the columnWidth int , String [] rowName, List <List <String >> dataList) throws IOException { // declare a workbook if (excelFile = null =) { ExcelFile HSSFWorkbook new new = (); } // generate the title style @SuppressWarnings ( "unused") HSSFCellStyle TitleStyle = getTitleStyle (ExcelFile); // generates a header line style @SuppressWarnings ( "unused") HSSFCellStyle titleRowStyle = getRowTitleStyle (ExcelFile); // data pattern generating HSSFCellStyle cellRowStyle = getCellStyle (ExcelFile); // highlight format HSSFCellStyle styleLight = styleLight (ExcelFile); int sheetNum = (int) dataList.size ( ) / 60000; IF (dataList.size () = 0% 60000) {! sheetNum ++; } examples / ** merged cell // header styling HSSFRow ROW0 = sheet.createRow (0); createMyCells (0,19 , ROW0, titleRowStyle); sheet.addMergedRegion (getMyRegion (0, 2, 0, 0)); addMyTitle (ROW0, 0, "name of follow-up"); sheet.addMergedRegion (getMyRegion (0, 2,. 1,. 1)); addMyTitle (ROW1, 3," extended time / month "); addMyTitle (ROW0,. 1, "the total amount owed the name"); sheet.addMergedRegion (getMyRegion (0, 0, 2,. 7)); addMyTitle (ROW0, 2, "extended debt"); sheet.addMergedRegion (getMyRegion (0, 0,. 8, 13 is)); addMyTitle (ROW0,. 8 "extended contract number"); sheet.addMergedRegion (getMyRegion (0, 0, 14, 18 is)); addMyTitle (ROW0, 14, "the number of contracts extended debt"); HSSFRow sheet.createRow ROW1 = (. 1); createMyCells (0,19, ROW1, titleRowStyle); sheet.addMergedRegion (getMyRegion (1, 2, 2, 2)); addMyTitle (ROW1, 2, "the total amount of overdue debt"); sheet.addMergedRegion (getMyRegion (1, 1 ,. 3,. 7)); addMyTitle (ROW1,. 8, "the total number of contracts extended"); sheet.addMergedRegion (getMyRegion (. 1, 2,. 8,. 8)); sheet.addMergedRegion (getMyRegion (. 1,. 1,. 9, 13 is)); addMyTitle (ROW1,. 9, "extended time / month"); sheet.addMergedRegion ( getMyRegion (. 1, 2, 14, 14)); addMyTitle (ROW1, 14, ". 1 million or less"); sheet.addMergedRegion (getMyRegion (. 1, 2, 15, 15)); addMyTitle (ROW1, 15, ". 1 million to 50,000 "); sheet.addMergedRegion (getMyRegion (. 1, 2, 16, 16)); addMyTitle (ROW1, 16," 10 million to 50,000 "); sheet.addMergedRegion (getMyRegion (. 1, 2,. 17,. 17 )); addMyTitle (ROW1,. 17, "10 million to 30 million"); sheet.addMergedRegion (getMyRegion (. 1, 2, 18 is, 18 is)); addMyTitle (ROW1, 18 is, "30 million or more"); */ for (int = 0 K; K <sheetNum;++ k) { // create a sheet Sheet = excelFile.createSheet HSSFSheet (); excelFile.setSheetName (excelFile.getNumberOfSheets () -. 1, sheetName + K + "", (Short). 1); sheet.setDefaultColumnWidth ((Short) (the columnWidth)); // the Build Excel IF (rowName = null && rowName.length> 0!) { // set header int = columnNum rowName.length; HSSFRow rowRowName = sheet.createRow (0); // Create a row at index position 0 // column head is provided the sheet of cells for (n-int = 0; n-<columnNum; n-++) { HSSFCell cellRowName rowRowName.createCell = ((Short) n-); // creates a corresponding number of column header cells cellRowName.setEncoding (HSSFCell.ENCODING_UTF_16); cellRowName.setCellType (HSSFCell. CELL_TYPE_STRING); cellRowName.setCellValue(rowName[n]);//设置列头单元格的值 cellRowName.setCellStyle(titleRowStyle); } } int rowIndex = 1; for(int index = k * 60000; index < dataList.size(); index++){ List<String> dl = dataList.get(index); HSSFRow row = sheet.createRow(rowIndex); HSSFCellStyle cellstyle = cellRowStyle; if(rowIndex%2==1){ cellstyle = styleLight; } for(int i = 0; i < dl.size(); i++) { addCell_whh(row, cellstyle, dl.get(i), i); } rowIndex++; } } return excelFile; } private static Region getMyRegion(int rf,int rt,int cf,int ct){ Region region = new Region(); region.setRowFrom(rf); region.setRowTo(rt); region.setColumnFrom((short) cf); region.setColumnTo((short) ct); return region; } private static void createMyCells(int from,int to,HSSFRow rowRowName, HSSFCellStyle titleRowStyle){ for(int i = from; i < to; i++){ HSSFCell cellRowName = rowRowName.createCell((short)i); cellRowName.setEncoding( HSSFCell.ENCODING_UTF_16 ); cellRowName.setCellType( HSSFCell.CELL_TYPE_STRING ); cellRowName.setCellStyle(titleRowStyle); } } public static void addCell_whh(HSSFRow row, HSSFCellStyle cellstyle, String text, int n){ HSSFCell cell = row.createCell((short)n); cell.setCellStyle(cellstyle); cell.setEncoding( HSSFCell.ENCODING_UTF_16 ); cell.setCellType( HSSFCell.CELL_TYPE_STRING ); cell.setCellValue(text); } /** * 生成标题样式 * @param workbook * @return */ public static HSSFCellStyle getTitleStyle(HSSFWorkbook workbook){ HSSFCellStyle style = workbook.createCellStyle(); // 设置这些样式 style.setFillForegroundColor(HSSFColor.LIGHT_ORANGE.index); style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style.setBorderBottom(HSSFCellStyle.BORDER_THIN); style.setBorderLeft(HSSFCellStyle.BORDER_THIN); style.setBorderRight(HSSFCellStyle.BORDER_THIN); style.setBorderTop(HSSFCellStyle.BORDER_THIN); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); style.setBottomBorderColor(HSSFColor.GREY_50_PERCENT.index); style.setRightBorderColor(HSSFColor.GREY_50_PERCENT.index); style.setLeftBorderColor(HSSFColor.GREY_50_PERCENT.index); style.setTopBorderColor(HSSFColor.GREY_50_PERCENT.index); // 生成一个字体 HSSFFont font = workbook.createFont(); font.setColor(HSSFColor.VIOLET.index); font.setFontHeightInPoints((short) 12); font.setBoldweight (HSSFFont.BOLDWEIGHT_BOLD); // font style applied to the current style.setFont(font); return style; } / ** * generate row headers style * @param Workbook * @return * / public static HSSFCellStyle getRowTitleStyle (HSSFWorkbook Workbook) { HSSFCellStyle style workbook.createCellStyle = (); // set these styles style.setFillForegroundColor (HSSFColor.LIGHT_ORANGE.index); style.setFillPattern (HSSFCellStyle.SOLID_FOREGROUND); style.setBorderBottom (HSSFCellStyle.BORDER_THIN); style.setBorderLeft (HSSFCellStyle.BORDER_THIN); style.setBorderRight (HSSFCellStyle.BORDER_THIN); style.setBorderTop (HSSFCellStyle.BORDER_THIN); style.setAlignment (HSSFCellStyle.ALIGN_CENTER); style.setBottomBorderColor(HSSFColor.GREY_50_PERCENT.index); style.setRightBorderColor (HSSFColor.GREY_50_PERCENT.index); style.setLeftBorderColor (HSSFColor.GREY_50_PERCENT.index); style.setTopBorderColor (HSSFColor.GREY_50_PERCENT.index); // generate a font HSSFFont font = workbook.createFont (); // font.setColor (HSSFColor.VIOLET.index); font.setFontHeightInPoints ((Short) 10); font.setBoldweight (HSSFFont.BOLDWEIGHT_BOLD); // font style applied to the current style.setFont (font); return style; } / ** * generated data pattern * @param Workbook * @return * / public static HSSFCellStyle getCellStyle (HSSFWorkbook Workbook) { HSSFCellStyle style = workbook.createCellStyle(); style.setFillForegroundColor(HSSFColor.WHITE.index); style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style.setBorderBottom(HSSFCellStyle.BORDER_THIN); style.setBorderLeft(HSSFCellStyle.BORDER_THIN); style.setBorderRight(HSSFCellStyle.BORDER_THIN); style.setBorderTop(HSSFCellStyle.BORDER_THIN); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); style.setBottomBorderColor(HSSFColor.GREY_50_PERCENT.index); style.setRightBorderColor(HSSFColor.GREY_50_PERCENT.index); style.setLeftBorderColor(HSSFColor.GREY_50_PERCENT.index); style.setTopBorderColor(HSSFColor.GREY_50_PERCENT.index); // 生成另一个字体 HSSFFont font = workbook.createFont(); font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); // 把字体应用到当前的样式 style.setFont(font); return style; } /** * 高亮格式 * @param workbook * @return */ public static HSSFCellStyle styleLight(HSSFWorkbook workbook){ HSSFCellStyle style = workbook.createCellStyle(); style.setFillForegroundColor(HSSFColor.PALE_BLUE.index); //HSSFColor.SKY_BLUE; style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style.setBorderBottom(HSSFCellStyle.BORDER_THIN); style.setBorderLeft(HSSFCellStyle.BORDER_THIN); style.setBorderRight(HSSFCellStyle.BORDER_THIN); style.setBorderTop (HSSFCellStyle.BORDER_THIN); style.setAlignment (HSSFCellStyle.ALIGN_CENTER); style.setVerticalAlignment (HSSFCellStyle.VERTICAL_CENTER); style.setBottomBorderColor (HSSFColor.GREY_50_PERCENT.index); style.setRightBorderColor (HSSFColor.GREY_50_PERCENT.index); style .setLeftBorderColor (HSSFColor.GREY_50_PERCENT.index); style.setTopBorderColor (HSSFColor.GREY_50_PERCENT.index); // generate another Font HSSFFont font workbook.createFont = (); font.setBoldweight (HSSFFont.BOLDWEIGHT_NORMAL); // use the font to the current style style.setFont (font); return style; } / ** * pre-export file name * @param str * @return */ public static String processExcelFilename(String str){ try { return java.net.URLEncoder.encode(str, "utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return "No Name"; } /** * @param args */ public static void main(String[] args) { // TODO 自动生成的方法存根 List<List<String>> dataList = new ArrayList<List<String>>(); System.out.println((int)100/6000); } }