本文主要实现利用总表向分表中添加数据,主界面如下:
在整个小项目中主要有一下几个关键步骤:Excel表的读取及转成datatable进行相关的数据操作,到后面datatable转成excel保存,
整个代码如下:
using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Data.OleDb;
using System.Windows.Forms;
<strong>using Microsoft.Office.Interop.Excel</strong>;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string detailPath1 = "";
string detailPath2 = "";
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog oFile1 = new OpenFileDialog();
oFile1.Filter = "Excel文件(*.xls)|*.xls";
if (oFile1.ShowDialog() == DialogResult.OK)
{
string oFName1 = oFile1.FileName.ToString();
detailPath1 = textBox1.Text = oFName1;
}
}
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog oFile2 = new OpenFileDialog();
oFile2.Filter = "Excel文件(*.xls)|*.xls";
if (oFile2.ShowDialog() == DialogResult.OK)
{
string oFName2 = oFile2.FileName.ToString();
detailPath2 = textBox2.Text = oFName2;
}
}
//CheckedListBox实现单选
private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.SetItemChecked(i, false);
}
if (checkedListBox1.CheckedItems == null)
{
checkedListBox1.SetItemChecked(checkedListBox1.SelectedIn