程序目的:EXCEL文件存放数据,根据提供的模板,生成相应的word文件,每条数据生成一页数据。
Excel操作:取数据
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace WorkBookForHKAccountDoc
{
public class ExcelOperation
{
public delegate void ErrorEventHandler(object sender, OnErrorEventArgs e);
public event ErrorEventHandler OnError = null;
public delegate void InfoEventHandler(object sender, OnPrintInfoEventArgs e);
public event InfoEventHandler OnPrintInfo = null;
获取EXCEL中用户信息列表
}
}
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace WorkBookForHKAccountDoc
{
public class ExcelOperation
{
public delegate void ErrorEventHandler(object sender, OnErrorEventArgs e);
public event ErrorEventHandler OnError = null;
public delegate void InfoEventHandler(object sender, OnPrintInfoEventArgs e);
public event InfoEventHandler OnPrintInfo = null;
获取EXCEL中用户信息列表
}
}
数据结构:
using System;
using System.Collections.Generic;
using System.Text;
namespace WorkBookForHKAccountDoc
{
public class AccountInfo
{
public string txtClient_Main_Account;
public string txtClient_Properity;
public string txtClient_Sub_Account;
public string txtShort_Name;
public string txtChinese_Name;
public string txtOpen_Date;
public string txtStatus;
}
}
using System.Collections.Generic;
using System.Text;
namespace WorkBookForHKAccountDoc
{
public class AccountInfo
{
public string txtClient_Main_Account;
public string txtClient_Properity;
public string txtClient_Sub_Account;
public string txtShort_Name;
public string txtChinese_Name;
public string txtOpen_Date;
public string txtStatus;
}
}
Word文件操作:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Word;
namespace WorkBookForHKAccountDoc
{
public class WordOperation
{
public delegate void ErrorEventHandler(object sender, OnErrorEventArgs e);
public event ErrorEventHandler OnError = null;
public delegate void InfoEventHandler(object sender, OnPrintInfoEventArgs e);
public event InfoEventHandler OnPrintInfo = null;
private int intCopyNum = 1;
生成用户信息文档
生成用户ID表格文档
CommonFunctions
}
}
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Word;
namespace WorkBookForHKAccountDoc
{
public class WordOperation
{
public delegate void ErrorEventHandler(object sender, OnErrorEventArgs e);
public event ErrorEventHandler OnError = null;
public delegate void InfoEventHandler(object sender, OnPrintInfoEventArgs e);
public event InfoEventHandler OnPrintInfo = null;
private int intCopyNum = 1;
生成用户信息文档
生成用户ID表格文档
CommonFunctions
}
}
MAINFORM:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WorkBookForHKAccountDoc
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
initializeComBox();
}
private void btnXlsFileUp_Click(object sender, EventArgs e)
{
OpenFileDialog file = new OpenFileDialog();
file.Title = "选择Excel资源";
file.Filter = "xls files(*.xls)|*.xls" +
"|All files(*.*)|*.*";
if (file.ShowDialog() == DialogResult.OK)
{
txtXlsFilePathName.Text = file.FileName;
}
file.Dispose();
}
private void btnXlsFileUp2_Click(object sender, EventArgs e)
{
OpenFileDialog file = new OpenFileDialog();
file.Title = "选择Excel资源";
file.Filter = "xls files(*.xls)|*.xls" +
"|All files(*.*)|*.*";
if (file.ShowDialog() == DialogResult.OK)
{
txtXlsFilePathName2.Text = file.FileName;
}
file.Dispose();
}
private void btnDocFileUp_Click(object sender, EventArgs e)
{
OpenFileDialog file = new OpenFileDialog();
file.Title = "选择Doc文档";
file.Filter = "doc files(*.doc)|*.doc" +
"|All files(*.*)|*.*";
if (file.ShowDialog() == DialogResult.OK)
{
txtDocFilePathName.Text = file.FileName;
}
file.Dispose();
}
private void btnDocAccountFileUp_Click(object sender, EventArgs e)
{
OpenFileDialog file = new OpenFileDialog();
file.Title = "选择Doc文档";
file.Filter = "doc files(*.doc)|*.doc" +
"|All files(*.*)|*.*";
if (file.ShowDialog() == DialogResult.OK)
{
this.txtDocAccountFilePathName.Text = file.FileName;
}
file.Dispose();
}
private void btnGenerateAccInfoDocs_Click(object sender, EventArgs e)
{
if (!validateTag1())
{
return;
}
try
{
ExcelOperation xlsOp = new ExcelOperation();
xlsOp.OnPrintInfo += new ExcelOperation.InfoEventHandler(OnPrintInfo);
xlsOp.OnError += new ExcelOperation.ErrorEventHandler(OnError);
System.Collections.Generic.List<AccountInfo> list = xlsOp.getAccountList(this.txtXlsFilePathName.Text);
int intTmp = 1;
if (comboBox1.SelectedIndex > 0)
{
intTmp = Convert.ToInt32(this.comboBox1.SelectedItem);
}
WordOperation docOp = new WordOperation();
docOp.OnError += new WordOperation.ErrorEventHandler(OnError);
docOp.OnPrintInfo += new WordOperation.InfoEventHandler(OnPrintInfo);
object objDocMouldFileAccInfo = txtDocFilePathName.Text;
docOp.generateDocumentDoc(list, GetConfigValue("strSaveAccountInfoDocPath"), objDocMouldFileAccInfo,intTmp);
MessageBox.Show("执行完毕!");
}
catch (Exception ex)
{
OnError(this, new OnErrorEventArgs("", ex));
}
}
private void btnGenerateAccIDDocs_Click(object sender, EventArgs e)
{
if (!validateTag2())
{
return;
}
try
{
ExcelOperation xlsOp = new ExcelOperation();
xlsOp.OnPrintInfo += new ExcelOperation.InfoEventHandler(OnPrintInfo);
xlsOp.OnError += new ExcelOperation.ErrorEventHandler(OnError);
System.Collections.Generic.List<AccountInfo> list = xlsOp.getAccountList(this.txtXlsFilePathName2.Text);
WordOperation docOp = new WordOperation();
docOp.OnError += new WordOperation.ErrorEventHandler(OnError);
docOp.OnPrintInfo += new WordOperation.InfoEventHandler(OnPrintInfo);
object objDocMouldFileAccID = txtDocAccountFilePathName.Text;
docOp.generateTableDoc(list, GetConfigValue("strSaveAccountIDDocPath"), objDocMouldFileAccID);
MessageBox.Show("执行完毕!");
}
catch (Exception ex)
{
OnError(this, new OnErrorEventArgs("", ex));
}
}
void OnError(object sender, OnErrorEventArgs e)
{
listBox1.Items.Add(e.AccountID + " | " + e.InnerException.Message);
this.Refresh();
}
void OnPrintInfo(object sender, OnPrintInfoEventArgs e)
{
listBox1.Items.Add(e.StrEventInfo);
listBox1.SelectedIndex = listBox1.Items.Count - 1;
this.Refresh();
}
void initializeComBox()
{
object objComBoxItem = "--用户信息拷贝数--";
this.comboBox1.Items.Add(objComBoxItem);
for (int i = 1; i <= 10; i++)
{
objComBoxItem = i;
this.comboBox1.Items.Add(objComBoxItem);
}
comboBox1.SelectedIndex = 0;
}
bool validateTag1()
{
if (this.txtDocAccountFilePathName.Text == "" || txtXlsFilePathName.Text == "")
{
MessageBox.Show("请选择需要的文件!");
return false;
}
else
{
return true;
}
}
bool validateTag2()
{
if ( this.txtDocAccountFilePathName.Text == "" || txtXlsFilePathName2.Text == "")
{
MessageBox.Show("请选择需要的文件!");
return false;
}
else
{
return true;
}
}
string GetConfigValue(string strCfgName)
{
string strRet = System.Configuration.ConfigurationSettings.AppSettings[strCfgName];
if (!strRet.EndsWith(@"\"))
{
strRet = strRet + @"\";
}
return strRet;
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WorkBookForHKAccountDoc
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
initializeComBox();
}
private void btnXlsFileUp_Click(object sender, EventArgs e)
{
OpenFileDialog file = new OpenFileDialog();
file.Title = "选择Excel资源";
file.Filter = "xls files(*.xls)|*.xls" +
"|All files(*.*)|*.*";
if (file.ShowDialog() == DialogResult.OK)
{
txtXlsFilePathName.Text = file.FileName;
}
file.Dispose();
}
private void btnXlsFileUp2_Click(object sender, EventArgs e)
{
OpenFileDialog file = new OpenFileDialog();
file.Title = "选择Excel资源";
file.Filter = "xls files(*.xls)|*.xls" +
"|All files(*.*)|*.*";
if (file.ShowDialog() == DialogResult.OK)
{
txtXlsFilePathName2.Text = file.FileName;
}
file.Dispose();
}
private void btnDocFileUp_Click(object sender, EventArgs e)
{
OpenFileDialog file = new OpenFileDialog();
file.Title = "选择Doc文档";
file.Filter = "doc files(*.doc)|*.doc" +
"|All files(*.*)|*.*";
if (file.ShowDialog() == DialogResult.OK)
{
txtDocFilePathName.Text = file.FileName;
}
file.Dispose();
}
private void btnDocAccountFileUp_Click(object sender, EventArgs e)
{
OpenFileDialog file = new OpenFileDialog();
file.Title = "选择Doc文档";
file.Filter = "doc files(*.doc)|*.doc" +
"|All files(*.*)|*.*";
if (file.ShowDialog() == DialogResult.OK)
{
this.txtDocAccountFilePathName.Text = file.FileName;
}
file.Dispose();
}
private void btnGenerateAccInfoDocs_Click(object sender, EventArgs e)
{
if (!validateTag1())
{
return;
}
try
{
ExcelOperation xlsOp = new ExcelOperation();
xlsOp.OnPrintInfo += new ExcelOperation.InfoEventHandler(OnPrintInfo);
xlsOp.OnError += new ExcelOperation.ErrorEventHandler(OnError);
System.Collections.Generic.List<AccountInfo> list = xlsOp.getAccountList(this.txtXlsFilePathName.Text);
int intTmp = 1;
if (comboBox1.SelectedIndex > 0)
{
intTmp = Convert.ToInt32(this.comboBox1.SelectedItem);
}
WordOperation docOp = new WordOperation();
docOp.OnError += new WordOperation.ErrorEventHandler(OnError);
docOp.OnPrintInfo += new WordOperation.InfoEventHandler(OnPrintInfo);
object objDocMouldFileAccInfo = txtDocFilePathName.Text;
docOp.generateDocumentDoc(list, GetConfigValue("strSaveAccountInfoDocPath"), objDocMouldFileAccInfo,intTmp);
MessageBox.Show("执行完毕!");
}
catch (Exception ex)
{
OnError(this, new OnErrorEventArgs("", ex));
}
}
private void btnGenerateAccIDDocs_Click(object sender, EventArgs e)
{
if (!validateTag2())
{
return;
}
try
{
ExcelOperation xlsOp = new ExcelOperation();
xlsOp.OnPrintInfo += new ExcelOperation.InfoEventHandler(OnPrintInfo);
xlsOp.OnError += new ExcelOperation.ErrorEventHandler(OnError);
System.Collections.Generic.List<AccountInfo> list = xlsOp.getAccountList(this.txtXlsFilePathName2.Text);
WordOperation docOp = new WordOperation();
docOp.OnError += new WordOperation.ErrorEventHandler(OnError);
docOp.OnPrintInfo += new WordOperation.InfoEventHandler(OnPrintInfo);
object objDocMouldFileAccID = txtDocAccountFilePathName.Text;
docOp.generateTableDoc(list, GetConfigValue("strSaveAccountIDDocPath"), objDocMouldFileAccID);
MessageBox.Show("执行完毕!");
}
catch (Exception ex)
{
OnError(this, new OnErrorEventArgs("", ex));
}
}
void OnError(object sender, OnErrorEventArgs e)
{
listBox1.Items.Add(e.AccountID + " | " + e.InnerException.Message);
this.Refresh();
}
void OnPrintInfo(object sender, OnPrintInfoEventArgs e)
{
listBox1.Items.Add(e.StrEventInfo);
listBox1.SelectedIndex = listBox1.Items.Count - 1;
this.Refresh();
}
void initializeComBox()
{
object objComBoxItem = "--用户信息拷贝数--";
this.comboBox1.Items.Add(objComBoxItem);
for (int i = 1; i <= 10; i++)
{
objComBoxItem = i;
this.comboBox1.Items.Add(objComBoxItem);
}
comboBox1.SelectedIndex = 0;
}
bool validateTag1()
{
if (this.txtDocAccountFilePathName.Text == "" || txtXlsFilePathName.Text == "")
{
MessageBox.Show("请选择需要的文件!");
return false;
}
else
{
return true;
}
}
bool validateTag2()
{
if ( this.txtDocAccountFilePathName.Text == "" || txtXlsFilePathName2.Text == "")
{
MessageBox.Show("请选择需要的文件!");
return false;
}
else
{
return true;
}
}
string GetConfigValue(string strCfgName)
{
string strRet = System.Configuration.ConfigurationSettings.AppSettings[strCfgName];
if (!strRet.EndsWith(@"\"))
{
strRet = strRet + @"\";
}
return strRet;
}
}
}