乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      java

       hongjing_z 2019-07-31
                                                           先開始接到這個需求的時候,第一想法是這得要多復雜的算才能實現(xiàn),因為不是每一列的合并的行數(shù)一樣,比如第一列1-4行的內容都相同,那么需要合并,而第二列1-2相同,所以1和2合并,3-4相同,3和4合并;然后一層一層的,就和樹一樣,逐層向下;
            毋庸置疑,通過算法肯定能實現(xiàn),但是我覺得太繞了,一下子也想不出來什么好的算法;后來我就想到約瑟夫環(huán)的問題,一個典型的java面向對象的例子;后來就通過建一個單元格對象,屬性有單元格內容,與單元格同一列的上一行內容,起始行,起始列,就這四個屬性;詳細解釋我放在代碼中:

      model對象:

      1. /**
      2. * Created by zelei.fan on 2017/3/20.
      3. */
      4. public class PoiModel {
      5. private String content;
      6. private String oldContent;
      7. private int rowIndex;
      8. private int cellIndex;
      9. public String getOldContent() {
      10. return oldContent;
      11. }
      12. public void setOldContent(String oldContent) {
      13. this.oldContent = oldContent;
      14. }
      15. public String getContent() {
      16. return content;
      17. }
      18. public void setContent(String content) {
      19. this.content = content;
      20. }
      21. public int getRowIndex() {
      22. return rowIndex;
      23. }
      24. public void setRowIndex(int rowIndex) {
      25. this.rowIndex = rowIndex;
      26. }
      27. public int getCellIndex() {
      28. return cellIndex;
      29. }
      30. public void setCellIndex(int cellIndex) {
      31. this.cellIndex = cellIndex;
      32. }
      33. }
      Test:
      1. import cn.yoho.perf.common.model.PoiModel;
      2. import com.beust.jcommander.internal.Maps;
      3. import com.google.common.collect.Lists;
      4. import org.apache.poi.ss.usermodel.Cell;
      5. import org.apache.poi.ss.usermodel.Row;
      6. import org.apache.poi.ss.usermodel.Sheet;
      7. import org.apache.poi.ss.usermodel.Workbook;
      8. import org.apache.poi.ss.util.CellRangeAddress;
      9. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
      10. import java.io.File;
      11. import java.io.FileOutputStream;
      12. import java.io.IOException;
      13. import java.util.*;
      14. /**
      15. * Created by zelei.fan on 2017/3/14.
      16. */
      17. public class Test{
      18. /**
      19. * @param title 標題集合 tilte的長度應該與list中的model的屬性個數(shù)一致
      20. * @param maps 內容集合
      21. * @param mergeIndex 合并單元格的列
      22. */
      23. public static String createExcel(String[] title, Map<String/*sheet名*/, List<Map<String/*對應title的值*/, String>>> maps, int[] mergeIndex){
      24. if (title.length==0){
      25. return null;
      26. }
      27. /*初始化excel模板*/
      28. Workbook workbook = new XSSFWorkbook();
      29. Sheet sheet = null;
      30. int n = 0;
      31. /*循環(huán)sheet頁*/
      32. for(Map.Entry<String, List<Map<String/*對應title的值*/, String>>> entry : maps.entrySet()){
      33. /*實例化sheet對象并且設置sheet名稱,book對象*/
      34. try {
      35. sheet = workbook.createSheet();
      36. workbook.setSheetName(n, entry.getKey());
      37. workbook.setSelectedTab(0);
      38. }catch (Exception e){
      39. e.printStackTrace();
      40. }
      41. /*初始化head,填值標題行(第一行)*/
      42. Row row0 = sheet.createRow(0);
      43. for(int i = 0; i<title.length; i++){
      44. /*創(chuàng)建單元格,指定類型*/
      45. Cell cell_1 = row0.createCell(i, Cell.CELL_TYPE_STRING);
      46. cell_1.setCellValue(title[i]);
      47. }
      48. /*得到當前sheet下的數(shù)據(jù)集合*/
      49. List<Map<String/*對應title的值*/, String>> list = entry.getValue();
      50. /*遍歷該數(shù)據(jù)集合*/
      51. List<PoiModel> poiModels = Lists.newArrayList();
      52. if(null!=workbook){
      53. Iterator iterator = list.iterator();
      54. int index = 1;/*這里1是從excel的第二行開始,第一行已經(jīng)塞入標題了*/
      55. while (iterator.hasNext()){
      56. Row row = sheet.createRow(index);
      57. /*取得當前這行的map,該map中以key,value的形式存著這一行值*/
      58. Map<String, String> map = (Map<String, String>)iterator.next();
      59. /*循環(huán)列數(shù),給當前行塞值*/
      60. for(int i = 0; i<title.length; i++){
      61. String old = "";
      62. /*old存的是上一行統(tǒng)一位置的單元的值,第一行是最上一行了,所以從第二行開始記*/
      63. if(index > 1){
      64. old = poiModels.get(i)==null?"":poiModels.get(i).getContent();
      65. }
      66. /*循環(huán)需要合并的列*/
      67. for(int j = 0; j < mergeIndex.length; j++){
      68. if(index == 1){
      69. /*記錄第一行的開始行和開始列*/
      70. PoiModel poiModel = new PoiModel();
      71. poiModel.setOldContent(map.get(title[i]));
      72. poiModel.setContent(map.get(title[i]));
      73. poiModel.setRowIndex(1);
      74. poiModel.setCellIndex(i);
      75. poiModels.add(poiModel);
      76. break;
      77. }else if(i > 0 && mergeIndex[j] == i){/*這邊i>0也是因為第一列已經(jīng)是最前一列了,只能從第二列開始*/
      78. /*當前同一列的內容與上一行同一列不同時,把那以上的合并, 或者在當前元素一樣的情況下,前一列的元素并不一樣,這種情況也合并*/
      79. /*如果不需要考慮當前行與上一行內容相同,但是它們的前一列內容不一樣則不合并的情況,把下面條件中||poiModels.get(i).getContent().equals(map.get(title[i])) && !poiModels.get(i - 1).getOldContent().equals(map.get(title[i-1]))去掉就行*/
      80. if(!poiModels.get(i).getContent().equals(map.get(title[i])) || poiModels.get(i).getContent().equals(map.get(title[i])) && !poiModels.get(i - 1).getOldContent().equals(map.get(title[i-1]))){
      81. /*當前行的當前列與上一行的當前列的內容不一致時,則把當前行以上的合并*/
      82. CellRangeAddress cra=new CellRangeAddress(poiModels.get(i).getRowIndex()/*從第二行開始*/, index - 1/*到第幾行*/, poiModels.get(i).getCellIndex()/*從某一列開始*/, poiModels.get(i).getCellIndex()/*到第幾列*/);
      83. //在sheet里增加合并單元格
      84. sheet.addMergedRegion(cra);
      85. /*重新記錄該列的內容為當前內容,行標記改為當前行標記,列標記則為當前列*/
      86. poiModels.get(i).setContent(map.get(title[i]));
      87. poiModels.get(i).setRowIndex(index);
      88. poiModels.get(i).setCellIndex(i);
      89. }
      90. }
      91. /*處理第一列的情況*/
      92. if(mergeIndex[j] == i && i == 0 && !poiModels.get(i).getContent().equals(map.get(title[i]))){
      93. /*當前行的當前列與上一行的當前列的內容不一致時,則把當前行以上的合并*/
      94. CellRangeAddress cra=new CellRangeAddress(poiModels.get(i).getRowIndex()/*從第二行開始*/, index - 1/*到第幾行*/, poiModels.get(i).getCellIndex()/*從某一列開始*/, poiModels.get(i).getCellIndex()/*到第幾列*/);
      95. //在sheet里增加合并單元格
      96. sheet.addMergedRegion(cra);
      97. /*重新記錄該列的內容為當前內容,行標記改為當前行標記*/
      98. poiModels.get(i).setContent(map.get(title[i]));
      99. poiModels.get(i).setRowIndex(index);
      100. poiModels.get(i).setCellIndex(i);
      101. }
      102. /*最后一行沒有后續(xù)的行與之比較,所有當?shù)阶詈笠恍袝r則直接合并對應列的相同內容*/
      103. if(mergeIndex[j] == i && index == list.size()){
      104. CellRangeAddress cra=new CellRangeAddress(poiModels.get(i).getRowIndex()/*從第二行開始*/, index/*到第幾行*/, poiModels.get(i).getCellIndex()/*從某一列開始*/, poiModels.get(i).getCellIndex()/*到第幾列*/);
      105. //在sheet里增加合并單元格
      106. sheet.addMergedRegion(cra);
      107. }
      108. }
      109. Cell cell = row.createCell(i, Cell.CELL_TYPE_STRING);
      110. cell.setCellValue(map.get(title[i]));
      111. /*在每一個單元格處理完成后,把這個單元格內容設置為old內容*/
      112. poiModels.get(i).setOldContent(old);
      113. }
      114. index++;
      115. }
      116. }
      117. n++;
      118. }
      119. /*生成臨時文件*/
      120. FileOutputStream out = null;
      121. String localPath = null;
      122. File tempFile = null;
      123. String fileName = String.valueOf(new Date().getTime()/1000);
      124. try {
      125. tempFile = File.createTempFile(fileName, ".xlsx");
      126. localPath = tempFile.getAbsolutePath();
      127. out = new FileOutputStream(localPath);
      128. workbook.write(out);
      129. }catch (IOException e){
      130. e.printStackTrace();
      131. }finally {
      132. try {
      133. out.flush();
      134. out.close();
      135. }catch (IOException e){
      136. e.printStackTrace();
      137. }
      138. }
      139. return localPath;
      140. }
      141. public static void main(String[] args) throws IOException{
      142. /*此處標題的數(shù)組則對應excel的標題*/
      143. String[] title = {"id","標題","描述","負責人","開始時間"};
      144. List<Map<String, String>> list = Lists.newArrayList();
      145. /*這邊是制造一些數(shù)據(jù),注意每個list中map的key要和標題數(shù)組中的元素一致*/
      146. for(int i = 0; i<10; i++){
      147. HashMap<String, String> map = com.google.common.collect.Maps.newHashMap();
      148. if(i > 5){
      149. if(i<7){
      150. map.put("id","333");
      151. map.put("標題","mmmm");
      152. }else {
      153. map.put("id","333");
      154. map.put("標題","aaaaa");
      155. }
      156. }else if (i >3){
      157. map.put("id","222");
      158. map.put("標題","哈哈哈哈");
      159. }else if (i>1 && i<3){
      160. map.put("id","222");
      161. map.put("標題","hhhhhhhh");
      162. }else {
      163. map.put("id","222");
      164. map.put("標題","bbbb");
      165. }
      166. map.put("描述","sssssss");
      167. map.put("負責人","vvvvv");
      168. map.put("開始時間","2017-02-27 11:20:26");
      169. list.add(map);
      170. }
      171. Map<String/*此處的key為每個sheet的名稱,一個excel中可能有多個sheet頁*/, List<Map<String/*此處key對應每一列的標題*/, String>>/*該list為每個sheet頁的數(shù)據(jù)*/> map = Maps.newHashMap();
      172. map.put("測試合并數(shù)據(jù)", list);
      173. System.out.println(createExcel(title, map, new int[]{0,1,2}/*此處數(shù)組為需要合并的列,可能有的需求是只需要某些列里面相同內容合并*/));
      174. }
      175. }

      生成的文件效果(兩種情況的):



        本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內容,請點擊一鍵舉報。
        轉藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多