日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

java

 hongjing_z 2019-07-31
                                                     先開(kāi)始接到這個(gè)需求的時(shí)候,第一想法是這得要多復(fù)雜的算才能實(shí)現(xiàn),因?yàn)椴皇敲恳涣械暮喜⒌男袛?shù)一樣,比如第一列1-4行的內(nèi)容都相同,那么需要合并,而第二列1-2相同,所以1和2合并,3-4相同,3和4合并;然后一層一層的,就和樹(shù)一樣,逐層向下;
      毋庸置疑,通過(guò)算法肯定能實(shí)現(xiàn),但是我覺(jué)得太繞了,一下子也想不出來(lái)什么好的算法;后來(lái)我就想到約瑟夫環(huán)的問(wèn)題,一個(gè)典型的java面向?qū)ο蟮睦樱缓髞?lái)就通過(guò)建一個(gè)單元格對(duì)象,屬性有單元格內(nèi)容,與單元格同一列的上一行內(nèi)容,起始行,起始列,就這四個(gè)屬性;詳細(xì)解釋我放在代碼中:

model對(duì)象:

  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 標(biāo)題集合 tilte的長(zhǎng)度應(yīng)該與list中的model的屬性個(gè)數(shù)一致
  20. * @param maps 內(nèi)容集合
  21. * @param mergeIndex 合并單元格的列
  22. */
  23. public static String createExcel(String[] title, Map<String/*sheet名*/, List<Map<String/*對(duì)應(yīng)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頁(yè)*/
  32. for(Map.Entry<String, List<Map<String/*對(duì)應(yīng)title的值*/, String>>> entry : maps.entrySet()){
  33. /*實(shí)例化sheet對(duì)象并且設(shè)置sheet名稱,book對(duì)象*/
  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,填值標(biāo)題行(第一行)*/
  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. /*得到當(dāng)前sheet下的數(shù)據(jù)集合*/
  49. List<Map<String/*對(duì)應(yīng)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的第二行開(kāi)始,第一行已經(jīng)塞入標(biāo)題了*/
  55. while (iterator.hasNext()){
  56. Row row = sheet.createRow(index);
  57. /*取得當(dāng)前這行的map,該map中以key,value的形式存著這一行值*/
  58. Map<String, String> map = (Map<String, String>)iterator.next();
  59. /*循環(huán)列數(shù),給當(dāng)前行塞值*/
  60. for(int i = 0; i<title.length; i++){
  61. String old = "";
  62. /*old存的是上一行統(tǒng)一位置的單元的值,第一行是最上一行了,所以從第二行開(kāi)始記*/
  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. /*記錄第一行的開(kāi)始行和開(kāi)始列*/
  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也是因?yàn)榈谝涣幸呀?jīng)是最前一列了,只能從第二列開(kāi)始*/
  78. /*當(dāng)前同一列的內(nèi)容與上一行同一列不同時(shí),把那以上的合并, 或者在當(dāng)前元素一樣的情況下,前一列的元素并不一樣,這種情況也合并*/
  79. /*如果不需要考慮當(dāng)前行與上一行內(nèi)容相同,但是它們的前一列內(nèi)容不一樣則不合并的情況,把下面條件中||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. /*當(dāng)前行的當(dāng)前列與上一行的當(dāng)前列的內(nèi)容不一致時(shí),則把當(dāng)前行以上的合并*/
  82. CellRangeAddress cra=new CellRangeAddress(poiModels.get(i).getRowIndex()/*從第二行開(kāi)始*/, index - 1/*到第幾行*/, poiModels.get(i).getCellIndex()/*從某一列開(kāi)始*/, poiModels.get(i).getCellIndex()/*到第幾列*/);
  83. //在sheet里增加合并單元格
  84. sheet.addMergedRegion(cra);
  85. /*重新記錄該列的內(nèi)容為當(dāng)前內(nèi)容,行標(biāo)記改為當(dāng)前行標(biāo)記,列標(biāo)記則為當(dāng)前列*/
  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. /*當(dāng)前行的當(dāng)前列與上一行的當(dāng)前列的內(nèi)容不一致時(shí),則把當(dāng)前行以上的合并*/
  94. CellRangeAddress cra=new CellRangeAddress(poiModels.get(i).getRowIndex()/*從第二行開(kāi)始*/, index - 1/*到第幾行*/, poiModels.get(i).getCellIndex()/*從某一列開(kāi)始*/, poiModels.get(i).getCellIndex()/*到第幾列*/);
  95. //在sheet里增加合并單元格
  96. sheet.addMergedRegion(cra);
  97. /*重新記錄該列的內(nèi)容為當(dāng)前內(nèi)容,行標(biāo)記改為當(dāng)前行標(biāo)記*/
  98. poiModels.get(i).setContent(map.get(title[i]));
  99. poiModels.get(i).setRowIndex(index);
  100. poiModels.get(i).setCellIndex(i);
  101. }
  102. /*最后一行沒(méi)有后續(xù)的行與之比較,所有當(dāng)?shù)阶詈笠恍袝r(shí)則直接合并對(duì)應(yīng)列的相同內(nèi)容*/
  103. if(mergeIndex[j] == i && index == list.size()){
  104. CellRangeAddress cra=new CellRangeAddress(poiModels.get(i).getRowIndex()/*從第二行開(kāi)始*/, index/*到第幾行*/, poiModels.get(i).getCellIndex()/*從某一列開(kāi)始*/, 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. /*在每一個(gè)單元格處理完成后,把這個(gè)單元格內(nèi)容設(shè)置為old內(nèi)容*/
  112. poiModels.get(i).setOldContent(old);
  113. }
  114. index++;
  115. }
  116. }
  117. n++;
  118. }
  119. /*生成臨時(shí)文件*/
  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. /*此處標(biāo)題的數(shù)組則對(duì)應(yīng)excel的標(biāo)題*/
  143. String[] title = {"id","標(biāo)題","描述","負(fù)責(zé)人","開(kāi)始時(shí)間"};
  144. List<Map<String, String>> list = Lists.newArrayList();
  145. /*這邊是制造一些數(shù)據(jù),注意每個(gè)list中map的key要和標(biāo)題數(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("標(biāo)題","mmmm");
  152. }else {
  153. map.put("id","333");
  154. map.put("標(biāo)題","aaaaa");
  155. }
  156. }else if (i >3){
  157. map.put("id","222");
  158. map.put("標(biāo)題","哈哈哈哈");
  159. }else if (i>1 && i<3){
  160. map.put("id","222");
  161. map.put("標(biāo)題","hhhhhhhh");
  162. }else {
  163. map.put("id","222");
  164. map.put("標(biāo)題","bbbb");
  165. }
  166. map.put("描述","sssssss");
  167. map.put("負(fù)責(zé)人","vvvvv");
  168. map.put("開(kāi)始時(shí)間","2017-02-27 11:20:26");
  169. list.add(map);
  170. }
  171. Map<String/*此處的key為每個(gè)sheet的名稱,一個(gè)excel中可能有多個(gè)sheet頁(yè)*/, List<Map<String/*此處key對(duì)應(yīng)每一列的標(biāo)題*/, String>>/*該list為每個(gè)sheet頁(yè)的數(shù)據(jù)*/> map = Maps.newHashMap();
  172. map.put("測(cè)試合并數(shù)據(jù)", list);
  173. System.out.println(createExcel(title, map, new int[]{0,1,2}/*此處數(shù)組為需要合并的列,可能有的需求是只需要某些列里面相同內(nèi)容合并*/));
  174. }
  175. }

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



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

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多