java 循环 输出 Object键值

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/wljk506/article/details/96478768
public class Demo {
    private Long id;
    private String name;
    private List<String>  format;
}

Demo demo = new Demo();
        demo.setFormat(list);
        demo.setName("xxxxxx");


         Object object =demo;
		 Field[] fields = object.getClass().getDeclaredFields();
        try {
            for (Field field : fields) {
                field.setAccessible(true);
                String name = field.getName();
                Object value = field.get(object);
                log.info(" key={}, value={}", name, value);
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

fox.风

展开阅读全文

怎么用java输出控制word循环

04-26

package word;nnimport java.io.File;nimport java.io.FileInputStream;nimport java.io.FileNotFoundException;nimport java.io.FileOutputStream;nimport java.io.IOException;nimport java.io.InputStream;nimport java.util.ArrayList;nimport java.util.HashMap;nimport java.util.Iterator;nimport java.util.List;nimport java.util.Map;nimport java.util.Map.Entry;nimport java.util.regex.Matcher;nimport java.util.regex.Pattern;nnimport org.apache.poi.POIXMLDocument;nimport org.apache.poi.hwpf.HWPFDocument;nimport org.apache.poi.hwpf.usermodel.Range;nimport org.apache.poi.xwpf.usermodel.XWPFDocument;nimport org.apache.poi.xwpf.usermodel.XWPFParagraph;nimport org.apache.poi.xwpf.usermodel.XWPFRun;nimport org.apache.poi.xwpf.usermodel.XWPFTable;nimport org.apache.poi.xwpf.usermodel.XWPFTableCell;nimport org.apache.poi.xwpf.usermodel.XWPFTableRow;nn/**n * @author wei.jian * n * @Created on 2014-10-28n * n */npublic class ReplaceVarInWordImpl nn // 返回Docx中需要替换的特殊字符,没有重复项n // 推荐传入正则表达式参数"\\$\\[^]+\\"n public ArrayList getReplaceElementsInWord(String filePath,n String regex) n String[] p = filePath.split("\\.");n if (p.length > 0) // 判断文件有无扩展名n // 比较文件扩展名n if (p[p.length - 1].equalsIgnoreCase("doc")) n ArrayList al = new ArrayList();n File file = new File(filePath);n HWPFDocument document = null;n try n InputStream is = new FileInputStream(file);n document = new HWPFDocument(is);n catch (FileNotFoundException e) n e.printStackTrace();n catch (IOException e) n e.printStackTrace();n n Range range = document.getRange();n String rangeText = range.text();n CharSequence cs = rangeText.subSequence(0, rangeText.length());n Pattern pattern = Pattern.compile(regex);n Matcher matcher = pattern.matcher(cs);n int startPosition = 0;n while (matcher.find(startPosition)) n if (!al.contains(matcher.group())) n al.add(matcher.group());n n startPosition = matcher.end();n n return al;n else if (p[p.length - 1].equalsIgnoreCase("docx")) n ArrayList al = new ArrayList();n XWPFDocument document = null;n try n document = new XWPFDocument(n POIXMLDocument.openPackage(filePath));n catch (IOException e) n e.printStackTrace();n n // 遍历段落n Iterator itPara = documentn .getParagraphsIterator();n while (itPara.hasNext()) n XWPFParagraph paragraph = (XWPFParagraph) itPara.next();n String paragraphString = paragraph.getText();n CharSequence cs = paragraphString.subSequence(0,n paragraphString.length());n Pattern pattern = Pattern.compile(regex);n Matcher matcher = pattern.matcher(cs);n int startPosition = 0;n while (matcher.find(startPosition)) n if (!al.contains(matcher.group())) n al.add(matcher.group());n n startPosition = matcher.end();n n n // 遍历表n Iterator itTable = document.getTablesIterator();n while (itTable.hasNext()) n XWPFTable table = (XWPFTable) itTable.next();n int rcount = table.getNumberOfRows();n for (int i = 0; i < rcount; i++) n XWPFTableRow row = table.getRow(i);n List cells = row.getTableCells();n for (XWPFTableCell cell : cells) n String cellText = "";n cellText = cell.getText();n CharSequence cs = cellText.subSequence(0,n cellText.length());n Pattern pattern = Pattern.compile(regex);n Matcher matcher = pattern.matcher(cs);n int startPosition = 0;n while (matcher.find(startPosition)) n if (!al.contains(matcher.group())) n al.add(matcher.group());n n startPosition = matcher.end();n n n n n return al;n else n return null;n n else n return null;n n nn // 替换word中需要替换的特殊字符n public static boolean replaceAndGenerateWord(String srcPath,n String destPath, Map map) n String[] sp = srcPath.split("\\.");n String[] dp = destPath.split("\\.");n if ((sp.length > 0) && (dp.length > 0)) // 判断文件有无扩展名n // 比较文件扩展名n if (sp[sp.length - 1].equalsIgnoreCase("docx")) n try n XWPFDocument document = new XWPFDocument(n POIXMLDocument.openPackage(srcPath));n // 替换段落中的指定文字n Iterator itPara = documentn .getParagraphsIterator();n while (itPara.hasNext()) n XWPFParagraph paragraph = (XWPFParagraph) itPara.next();n List runs = paragraph.getRuns();n for (int i = 0; i < runs.size(); i++) n String oneparaString = runs.get(i).getText(n runs.get(i).getTextPosition());n for (Map.Entry entry : mapn .entrySet()) n oneparaString = oneparaString.replace(n entry.getKey(), entry.getValue());n n runs.get(i).setText(oneparaString, 0);n n nn // 替换表格中的指定文字n Iterator itTable = document.getTablesIterator();n while (itTable.hasNext()) n XWPFTable table = (XWPFTable) itTable.next();n int rcount = table.getNumberOfRows();n for (int i = 0; i < rcount; i++) n XWPFTableRow row = table.getRow(i);n List cells = row.getTableCells();n for (XWPFTableCell cell : cells) n String cellTextString = cell.getText();n for (Entry e : map.entrySet()) n if (cellTextString.contains(e.getKey()))n cellTextString = cellTextStringn .replace(e.getKey(),n e.getValue());n n cell.removeParagraph(0);n cell.setText(cellTextString);n n n n FileOutputStream outStream = null;n outStream = new FileOutputStream(destPath);n document.write(outStream);n outStream.close();n return true;n catch (Exception e) n e.printStackTrace();n return false;n nn elsen // doc只能生成doc,如果生成docx会出错n if ((sp[sp.length - 1].equalsIgnoreCase("doc"))n && (dp[dp.length - 1].equalsIgnoreCase("doc"))) n HWPFDocument document = null;n try n document = new HWPFDocument(new FileInputStream(srcPath));n Range range = document.getRange();n for (Map.Entry entry : map.entrySet()) n range.replaceText(entry.getKey(), entry.getValue());n n FileOutputStream outStream = null;n outStream = new FileOutputStream(destPath);n document.write(outStream);n outStream.close();n return true;n catch (FileNotFoundException e) n e.printStackTrace();n return false;n catch (IOException e) n e.printStackTrace();n return false;n n else n return false;n n else n return false;n n nn public static void main(String[] args) n // TODO Auto-generated method stubn String filepathString = "e:/Doc1.docx";n String destpathString = "e:/Doc5.docx";n Map map = new HashMap(); n map.put("$Wenyang", "温飏");n System.out.println(replaceAndGenerateWord(filepathString,n destpathString,map));n nn n 上面是代码。n 例如:班级学生人数不同要根据人数停止输出内容 问答

没有更多推荐了,返回首页