c# 根据excel数据列表批量输出word文档

c# 根据excel数据列表批量输出word文档
在这里插入图片描述

using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Word;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Application = System.Windows.Forms.Application;
using excel = Microsoft.Office.Interop.Excel;
using MSWord = Microsoft.Office.Interop.Word;

namespace _12_输出资料
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button3_Click(object sender, EventArgs e)
{
try
{
if (checkBox1.Checked == false && checkBox2.Checked == false)
{
MessageBox.Show(“两个选项,需要选择一个。”);
return;
}
object missing = System.Reflection.Missing.Value;
excel.Application excel = new excel.Application();//lauch excel application
if (excel == null)
{
MessageBox.Show(“启动excel失败。”);
}
else
{
string strFileName = textBox1.Text;
excel.Visible = false; excel.UserControl = true;
// 以只读的形式打开EXCEL文件
Workbook wb = excel.Application.Workbooks.Open(strFileName, missing, true, missing, missing, missing,
missing, missing, missing, true, missing, missing, missing, missing, missing);
//取得第一个工作薄
Worksheet ws = (Worksheet)wb.Worksheets.get_Item(1);
//取得总记录行数 (包括标题列)
int rowsint = ws.UsedRange.Cells.Rows.Count; //得到行数
//int columnsint = mySheet.UsedRange.Cells.Columns.Count;//得到列数
System.Diagnostics.Trace.WriteLine(rowsint);
//设置进度条的最小及最大值
progressBar1.Maximum = rowsint - 1;
progressBar1.Minimum = 0;
int ii = 0;
for (int i = 2; i < rowsint + 1; i++)
{
string lhs = ws.Rows[i].Cells[1].Value;
string jjs = ws.Rows[i].Cells[2].Value;
string jh = ws.Rows[i].Cells[3].Value.ToString();
string hz = ws.Rows[i].Cells[4].Value;
string zjqsqk = ws.Rows[i].Cells[5].Value;
try
{
if (checkBox1.Checked == true)
output_sqb(lhs, jjs, jh, hz, zjqsqk);
if (checkBox2.Checked == true)
output_sqb1(lhs, jjs, jh, hz, zjqsqk);
richTextBox1.AppendText(jh + “-输出成功\n”);
}
catch
{
richTextBox1.AppendText(jh + “-输出错误\n”);
}
setPos(ii);
ii++;

}
setPos(ii);
}
}
catch
{ }
}
private void output_sqb(string lhs, string jjs, string jh, string hz, string zjqsqk)
{
object path;
string strContent;
MSWord.Application wordApp;
MSWord.Document wordDoc;
string curPath = Path.GetDirectoryName(this.GetType().Assembly.Location) + “\”;
path = curPath + “temp\附件1:土地确权资料缺失说明(缺失身份证、户口薄复印件材料)模板.doc”;
wordApp = new MSWord.Application(); //初始化

wordApp.Visible = false;//使文档可见
Object Nothing = Missing.Value;
//打开word表格
wordDoc = wordApp.Documents.Add(path);
//word文档中的查找替换
wordApp.Selection.Find.Execute(“A1”, null, null, null, null, null, null, 1, null, lhs, 2);
wordApp.Selection.Find.Execute(“B1”, null, null, null, null, null, null, 1, null, jjs, 2);
wordApp.Selection.Find.Execute(“C1”, null, null, null, null, null, null, 1, null, jh, 2);
wordApp.Selection.Find.Execute(“D1”, null, null, null, null, null, null, 1, null, hz, 2);
string path_bc = textBox2.Text;
wordDoc.SaveAs(path_bc + “\” + jh + “附件1:土地确权资料缺失说明(缺失身份证、户口薄复印件材料)模板.doc”);
//关闭word
wordApp.Quit();
}

private void output_sqb1(string lhs, string jjs, string jh, string hz, string zjqsqk)
{
object path;
string strContent;
MSWord.Application wordApp;
MSWord.Document wordDoc;
string curPath = Path.GetDirectoryName(this.GetType().Assembly.Location) + “\”;
path = curPath + “temp\附件2:土地确权资料缺失说明(缺失整件材料)模板.doc”;
wordApp = new MSWord.Application(); //初始化

wordApp.Visible = false;//使文档可见
Object Nothing = Missing.Value;
//打开word表格
wordDoc = wordApp.Documents.Add(path);
//word文档中的查找替换
wordApp.Selection.Find.Execute(“A1”, null, null, null, null, null, null, 1, null, lhs, 2);
wordApp.Selection.Find.Execute(“B1”, null, null, null, null, null, null, 1, null, jjs, 2);
wordApp.Selection.Find.Execute(“C1”, null, null, null, null, null, null, 1, null, jh, 2);
wordApp.Selection.Find.Execute(“D1”, null, null, null, null, null, null, 1, null, hz, 2);
string path_bc = textBox2.Text;
wordDoc.SaveAs(path_bc + “\” + jh + “附件2:土地确权资料缺失说明(缺失整件材料)模板.doc”);
//关闭word
wordApp.Quit();
}

private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Multiselect = false;//是否可以选择多个文件
dialog.Title = “请选择文件”;
dialog.Filter = “所有文件(.)|.”;
if (dialog.ShowDialog() == DialogResult.OK)
{
string filename = dialog.FileName;
textBox1.Text = filename;
}
}

private void button2_Click(object sender, EventArgs e)
{
string Path = “”;
FolderBrowserDialog folder = new FolderBrowserDialog();
folder.Description = “选择文件所在文件夹目录”; //提示的文字
folder.ShowDialog();
if (folder.ShowDialog() == DialogResult.OK)
{
Path = folder.SelectedPath;
textBox2.Text = Path;
}
}

private void label2_Click(object sender, EventArgs e)
{

    }

private void progressBar1_Click(object sender, EventArgs e)
{
}
private void setPos(int value) //设置进度条当前进度值
{
if (value < progressBar1.Maximum + 1) //如果值有效
{
progressBar1.Value = value; //设置进度值
}
Application.DoEvents();//重点,必须加上,否则父子窗体都假死
}

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked == true)
{
checkBox2.Checked = false;
}

    }

private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
if (checkBox2.Checked == true)
{
checkBox1.Checked = false;
}
}
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DK业

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值