c#访问EXCEL2003问题

来源:百度文库 编辑:神马文学网 时间:2024/04/29 15:43:37
这段代码对你也许有用。
 
v1.4 published CU feedback : perfect
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Data.OleDb;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
using System.Threading;
namespace Excel_Spliter
{
public partial class Form1 : Form
{
private string excelFilePath = "";
private string fileName;
private string folderPath = "";
private int cntOfColumns;
private int cntOfRows;
private int indexOfKey;
private int state = 0;
private string[] Columns = new string[500];
private bool[] type = new bool[500];
private string CreateCMD;
private string InsertTMP;
DataTable tb = new DataTable();
public Form1()
{
InitializeComponent();
}
private bool Bind()
{
OleDbConnection myConn = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + excelFilePath + ";Extended Properties=Excel 8.0");
myConn.Open();
string myCMD = " SELECT * FROM [sheet1$] ";
OleDbCommand cmd = new OleDbCommand(myCMD, myConn);
OleDbDataReader myReader = cmd.ExecuteReader();
if (!myReader.Read() ) return false;
cntOfColumns = myReader.FieldCount;
for (int i = 0; i < cntOfColumns;i++ )
{
if (myReader.IsDBNull(i))
{
cntOfColumns = i;
break;
}
Columns[i] = myReader.GetName(i);
DataColumn myColumn = new DataColumn(Columns[i]);
tb.Columns.Add(myColumn);
type[i] = myReader.GetValue(i).GetType() == System.Type.GetType("System.String");
string name = "";
for (int j = 0; j < Columns[i].Length; j++)
{
if (Columns[i][j] != ' ') name += Columns[i][j];
}
if (type[i])
{
listBox1.Items.Add(name + "(Text)");
}
else
{
listBox1.Items.Add(name + "(Data)");
}
}
cntOfRows = 0;
do
{
tb.Rows.Add();
for (int i = 0; i < cntOfColumns; i++)
{
tb.Rows[cntOfRows][i] = myReader.GetValue(i);
}
cntOfRows++;
}
while (myReader.Read());
progressBar1.Maximum = cntOfRows;
progressBar1.Value = progressBar1.Minimum = 0;
progressBar1.Step = 1;
myReader.Close();
myConn.Close();
listBox1.SelectedIndex = -1;
label2.Visible = true;
return true;
}
private void button1_Click(object sender, EventArgs e)
{
switch (state)
{
case 0:
{
excelFilePath = textBox1.Text;
if (!System.IO.File.Exists(excelFilePath))
{
MessageBox.Show("The file do not exist");
return;
}
if (!Bind())
{
MessageBox.Show("There is some error data in your file");
return;
}
label1.Text = "Please chose the target folder";
textBox1.Text = folderPath;
int begin = excelFilePath.LastIndexOf(@"\");
int end = excelFilePath.LastIndexOf(".xls");
fileName = excelFilePath.Substring(begin, end - begin);
state = 1;
}
break;
case 1:
{
folderPath = textBox1.Text;
if (!System.IO.Directory.Exists(folderPath))
{
MessageBox.Show("The folder do not exist");
return;
}
label2.Text = "Please check the type";
listBox1.Visible = true;
label1.Visible = false;
textBox1.Visible = false;
button2.Visible = false;
button3.Visible = true;
state = 2;
}
break;
case 2:
{
label2.Text = "Please choose the key word";
for (int i = 0; i < listBox1.Items.Count; i++)
{
listBox1.Items[i] = listBox1.Items[i].ToString().Substring(0,listBox1.Items[i].ToString().LastIndexOf("("));
}
listBox1.SelectedIndex = -1;
state = 3;
button3.Visible = false;
button1.Text = "Split";
}
break;
case 3:
{
indexOfKey = listBox1.SelectedIndex;
if (indexOfKey == -1)
{
MessageBox.Show("Please chose a key word");
return;
}
label2.Visible = false;
listBox1.Visible = false;
button1.Text = "Spliting";
progressBar1.Visible = true;
Process();
button1.Text = "Close";
state = 4;
}
break;
case 4:
{
this.Close();
}
break;
}
}
private void Process()
{
CreateCMD = "CREATE TABLE sheet1 ( ";
for (int i = 0; i < cntOfColumns; i++)
{
if (type[i])
{
CreateCMD += "[" + Columns[i] + "] char (10) ,";
}
else
{
CreateCMD += "[" + Columns[i] + "] float ,";
}
}
CreateCMD = CreateCMD.Substring(0, CreateCMD.Length - 1) + ")";
InsertTMP = "INSERT INTO sheet1 (";
for (int i = 0; i < cntOfColumns - 1; i++)
{
InsertTMP += "[" + Columns[i] + "],";
}
InsertTMP += "[" + Columns[cntOfColumns - 1] + "]) VALUES (";
for (int i = 0; i < cntOfRows;i++ )
{
if ( tb.Rows[i][indexOfKey].ToString() != "" && !System.IO.File.Exists(folderPath + @"\" + fileName + '-' + tb.Rows[i][indexOfKey].ToString() + ".xls"))
{
CreateAndInsert(i);
}
}
}
private void CreateAndInsert(int index)
{
string key = tb.Rows[index][indexOfKey].ToString().ToLower();
OleDbConnection objConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + folderPath + @"\" + fileName + '-' + key + ".xls;Extended Properties=Excel 8.0;");
objConn.Open();
OleDbCommand objCmd = new OleDbCommand(CreateCMD,objConn);
try
{
objCmd.ExecuteNonQuery();
}
catch
{
}
do
{
if(tb.Rows[index][indexOfKey].ToString().ToLower() == key)
{
string InsertCMD = InsertTMP;
for(int i= 0;i{
if(type[i])
{
InsertCMD += @"'" + tb.Rows[index][i].ToString() + @"',";
}
else
{
InsertCMD += tb.Rows[index][i].ToString() + ',';
}
}
if(type[cntOfColumns-1])
{
InsertCMD += @"'" + tb.Rows[index][cntOfColumns-1].ToString() + @"')";
}
else
{
InsertCMD += tb.Rows[index][cntOfColumns-1].ToString() + ')';
}
objCmd.CommandText = InsertCMD;
objCmd.ExecuteNonQuery();
progressBar1.PerformStep();
}
index++;
}
while(index < cntOfRows);
objConn.Close();
}
private void button2_Click(object sender, EventArgs e)
{
switch (state)
{
case 0:
{
excelFilePath = textBox1.Text;
openFileDialog1.Filter = "(*.xls)|*.xls";
openFileDialog1.Title = "Please select a excel file";
openFileDialog1.ShowDialog();
int begin = excelFilePath.LastIndexOf(@"\");
folderPath = excelFilePath.Substring(0, begin);
textBox1.Text = excelFilePath;
}
break;
case 1:
{
folderPath = textBox1.Text;
folderBrowserDialog1.ShowDialog();
folderPath = folderBrowserDialog1.SelectedPath; textBox1.Text = folderPath;
}
break;
}
}
private bool CheckFilePath()
{
return excelFilePath.EndsWith(".xls");
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
excelFilePath = openFileDialog1.FileName;
textBox1.Text = excelFilePath;
}
private void button3_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex == -1)
{
MessageBox.Show("Please chose from the list first");
return;
}
else
{
int now = listBox1.SelectedIndex;
type[now] = !type[now];
if (type[now])
{
listBox1.Items[now] = listBox1.Items[now].ToString().Substring(0, listBox1.Items[now].ToString().LastIndexOf("(")) + "(text)";
}
else
{
listBox1.Items[now] = listBox1.Items[now].ToString().Substring(0, listBox1.Items[now].ToString().LastIndexOf("(")) + "(data)";
}
}
}
}
}
--------------------------------------------------------------------------------
功能说明:将excel 文件按照其中某一列拆分,列属性相同的分致一个新的文件中,表格形式不变。