基于System.Text导出

public void WriteTxt()
{
string ACellNameChecked = "";
string InSysInterAnalyReportWordSavePath;
ACellNameChecked = InvalidCellNameCharsRemoveForFilePath(AnalysisCellNamecomboBox.Text);
if (!Directory.Exists(InterferenceAnalysisResultSavePath + "\\各小区优化方案报告"))
Directory.CreateDirectory(InterferenceAnalysisResultSavePath + "\\各小区优化方案报告");
InSysInterAnalyReportWordSavePath = InterferenceAnalysisResultSavePath + "\\各小区优化方案报告" + "\\" + ACellNameChecked + "网内干扰优化方案.txt";

FileStream FS = new FileStream(InSysInterAnalyReportWordSavePath, FileMode.Create);
StreamWriter SW = new StreamWriter(FS);

string Box = AnalysisCellNamecomboBox.Text;
string AcellName = OptimizeDictionaryAl[Box].Keys.ToList()[0];
string NcellName1 = OptimizeDictionaryAl[Box].Keys.ToList()[1];
string NcellName2 = OptimizeDictionaryAl[Box].Keys.ToList()[2];
string NcellName3 = OptimizeDictionaryAl[Box].Keys.ToList()[3];

StringBuilder ACell = new System.Text.StringBuilder("受扰小区:" + AcellName + ";");
StringBuilder Ncell1 = new System.Text.StringBuilder("干扰源小区1:" + NcellName1 + ";");
StringBuilder Ncell2 = new System.Text.StringBuilder("干扰源小区2:" + NcellName2 + ";");
StringBuilder Ncell3 = new System.Text.StringBuilder("干扰源小区3:" + NcellName3 + ";");


SW.Write(txt(ACell, Box, AcellName, "a") + "\r\n");
SW.Write(txt(Ncell1, Box, NcellName1, "b") + "\r\n");
SW.Write(txt(Ncell2, Box, NcellName2, "a") + "\r\n");
SW.Write(txt(Ncell3, Box, NcellName3, "b") + "\r\n");

SW.Flush(); //清空缓冲区
SW.Close(); //关闭流
FS.Close();
}

转载于:https://www.cnblogs.com/SiSui/p/9506132.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
poi-tl是一个基于Apache POI的Word模板引擎,可以使用Word模板和数据创建漂亮的Word文档。下面是一个使用poi-tl进行Word模板导出的示例: ```java import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import org.apache.poi.xwpf.usermodel.XWPFTable; import org.apache.poi.xwpf.usermodel.XWPFTableRow; import org.apache.poi.xwpf.usermodel.XWPFTableCell; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import java.io.FileOutputStream; import java.util.HashMap; import java.util.Map; public class WordTemplateExport { public static void main(String[] args) throws Exception { // 加载Word模板 XWPFDocument document = new XWPFDocument(WordTemplateExport.class.getResourceAsStream("template.docx")); // 获取模板中的段落和表格 XWPFParagraph paragraph = document.getParagraphs().get(0); XWPFTable table = document.getTables().get(0); // 替换段落中的占位符 Map<String, String> placeholders = new HashMap<>(); placeholders.put("title", "Hi, poi-tl Word模板引擎"); replacePlaceholder(paragraph, placeholders); // 替换表格中的占位符 Map<String, String> tablePlaceholders = new HashMap<>(); tablePlaceholders.put("name", "John Doe"); tablePlaceholders.put("age", "30"); replacePlaceholder(table, tablePlaceholders); // 导出Word文档 FileOutputStream out = new FileOutputStream("output.docx"); document.write(out); out.close(); System.out.println("Word模板导出成功!"); } // 替换段落中的占位符 private static void replacePlaceholder(XWPFParagraph paragraph, Map<String, String> placeholders) { List<XWPFRun> runs = paragraph.getRuns(); for (int i = 0; i < runs.size(); i++) { XWPFRun run = runs.get(i); String text = run.getText(0); if (text != null) { for (Map.Entry<String, String> entry : placeholders.entrySet()) { String placeholder = "${" + entry.getKey() + "}"; if (text.contains(placeholder)) { text = text.replace(placeholder, entry.getValue()); run.setText(text, 0); } } } } } // 替换表格中的占位符 private static void replacePlaceholder(XWPFTable table, Map<String, String> placeholders) { for (XWPFTableRow row : table.getRows()) { for (XWPFTableCell cell : row.getTableCells()) { for (XWPFParagraph paragraph : cell.getParagraphs()) { replacePlaceholder(paragraph, placeholders); } } } } } ``` 这个示例中,我们首先加载了一个Word模板文件,然后使用`replacePlaceholder`方法替换模板中的占位符。最后,将替换后的文档导出为一个新的Word文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值