using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
//**********************************************
//程式设计:殷庆飞
//设计时间:2007-8-10
//功能描述:读取Excel中的内容,并写入txt文件中
//修改人:
//修改描述:
//修改时间:
//修改描述:
//***********************************************
namespace 文件格式转换
{
public partial class Form1 : Form
{
OleDbConnection cn;
DataSet MyDataSet;
public Form1()
{
InitializeComponent();
}
//浏览按钮,选择要读取的EXCEL文件
private void btnFind_Click(object sender, EventArgs e)
{
txtName.Text = "";
OpenFileDialog openFile = new OpenFileDialog();
openFile.Filter = "Excel files (*.xls)|*.xls|All files (*.*)|*.*";
openFile.ShowDialog();
if (txtName.Text == string.Empty) txtName.Text = openFile.FileName;
}
//转换按钮,将Excel中的内容转换到文本文件中
private void btnConvert_Click(object sender, EventArgs e)
{
try
{
if (txtName.Text == string.Empty || txtConName.Text == string.Empty)//判断是否选择了要读到的Excel,是否输入了转换后的txt文件名称
{
MessageBox.Show("注意:选择要转换的Excel,并输入转换后的文本文件名称", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
cn = GetExcelConnection(txtName.Text.Trim(), false, 1);
if (cn != null)
{
cn.Open();
MyDataSet = ExecuteDataSet(cn);
if (MyDataSet != null)
{
string strPath = Application.StartupPath + @"\" + txtConName.Text.Trim() + ".txt";
if (File.Exists(strPath))
{
if (MessageBox.Show("文件:" + txtConName.Text + "已经存在!是否要删除?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
{
File.Delete(strPath);
}
else
{
MessageBox.Show("请输入其它的名称", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
txtConName.Focus();
return;
}
}
FileStream fs = new FileStream(strPath, FileMode.CreateNew, FileAccess.Write, FileShare.Read);
StreamWriter sw = new StreamWriter(fs);
string strContent = "";
for (int i = 0; i < MyDataSet.Tables.Count; i++)//Excel中sheet的个数
{
for (int row = 0; row < MyDataSet.Tables[i].Rows.Count; row++)
{
strContent = "";
for (int col = 0; col < MyDataSet.Tables[i].Columns.Count; col++)//sheet中列的个数
{
if (col == 1)
{
strContent += MyDataSet.Tables[i].Rows[row][col].ToString() + " ";
}
else
{
strContent += MyDataSet.Tables[i].Rows[row][col].ToString();
}
}
sw.WriteLine(strContent);
}
}
sw.Close();//关闭文件
MessageBox.Show("转换成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else
{
MessageBox.Show("连接Excel发生错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
catch (Exception ex)
{
string strError = string.Format("错误:{0}", ex.Message);
MessageBox.Show(strError, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
//退出
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
/**//// <summary>
/// 获取 Excel 连接对象。
/// </summary>
/// <param name="strFullPath">文件的完全路径</param>
/// <param name="isTreatedHeader">是否处理表头</param>
/// <param name="intIMEXMode">输入输出模式。1:设置输入为文本 Text 类型,通常使用该值。0/2:设置输入为 多数 Majority 类型,此设置极易导致数据缺失发生。</param>
/// <returns>Excel 连接对象</returns>
public static OleDbConnection GetExcelConnection( string strFullPath, bool isTreatedHeader, int intIMEXMode )
{
try
{
string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR={1};IMEX={2};'";
string strTreatedHeader = string.Empty;
if (isTreatedHeader) strTreatedHeader = "Yes";
else strTreatedHeader = "No";
connectionString = string.Format(connectionString, strFullPath, strTreatedHeader, intIMEXMode);
return new OleDbConnection(connectionString);
}
catch (Exception ex)
{
string strError = string.Format("错误:{0}", ex.Message);
MessageBox.Show(strError, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return null;
}
}
/**//// <summary>
/// 读取给定连接中全部或给定表的内容至 DataSet。
/// </summary>
/// <param name="cn">给定连接</param>
/// <param name="sheetNames">[可选参数]指定表名 的 sheet</param>
/// <returns>包含全部或给定 Sheet 数据的 DataSet</returns>
public static DataSet ExecuteDataSet( OleDbConnection cn, params string[] sheetNames )
{
try
{
DataSet ds = new DataSet();
DataTable schemaTable = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
string queryString;
// 带 $ 的表名
string fullTableName;
// 不带 $ 的表名
string realTableName;
OleDbDataAdapter odda = new OleDbDataAdapter();
foreach (DataRow dr in schemaTable.Rows)
{
fullTableName = dr["TABLE_NAME"].ToString();
realTableName = fullTableName.Remove(fullTableName.Length - 1, 1);
// 根据给定表导入
if (sheetNames.Length > 0)
{
// 若当前表不在给定表数组中,则不填充到数据集中。
if (Array.IndexOf(sheetNames, realTableName) < 0)
{
continue;
}
}
queryString = string.Format("SELECT * FROM [{0}]", fullTableName);
odda.SelectCommand = new OleDbCommand(queryString, cn);
odda.Fill(ds, realTableName);
}
return ds;
}
catch (Exception ex)
{
string strError = string.Format("错误:{0}", ex.Message);
MessageBox.Show(strError, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return null;
}
}
//打开关于框
private void btnAbout_Click(object sender, EventArgs e)
{
frmAbout frm = new frmAbout();
frm.ShowDialog();
}
}
}
可到这里去下载http://download.csdn.net/source/225379
分享到:
相关推荐
主要介绍了python3 循环读取excel文件并写入json操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
本文实例讲述了Python实现读取txt文件并转换为excel的方法。分享给大家供大家参考,具体如下: 这里的txt文件内容格式为: 892天平天国定都在?A开封B南京C北京(B) Python代码如下: # coding=utf-8 ''''' main...
利用vb读取ini文件,并写入excel
读取目录中的xml文件并写入Excel,很好的例子,用到了 Aspose
NPOI操作excel,批量读取excel文件内容,再打开excel模板,写入相应的内容
C# 编写的操作Excel读取/写入动态库(COM/NPOI两种方式),个人推荐使用NPOI方式,因为COM方式存在无法关闭Excel进程的情况(个人体验COM方式效率有点低)。。。已设置0分,资源自取。
该程序主要实现了从XMl文件中读取数据,并按照要求格式写进Excel表格里面
这个项目实现的功能是读取excel文件中的数据,解析并写入数据库。 读取的excel文件位于项目目录下的 excel\0805.xlsx 使用IntelliJ IDEA开发此项目 使用MYSQL查看数据库 在MYSQL中运行项目db目录下的sql文件,创建...
可以直接拿来用
批量读取同一文件夹下所有excel指定单元格内容并写入新单元格
Python的读取Excel文件,做一定筛选,读取行和列;python写入文档;
用sql server读取和写入excel文件,读取和写入数据非常方便,大批量数据导入与导出非常快
Android开发时,需要把数据存储在Excel文件中,并且用时还可以读取。支持大数据的存储和读取。
java读取和写入EXCEL文件,在myeclipse平台上运行!!
批量读取excel文件并利用dbcp连接池写入数据库
1、java解析读取excel文件中的数据,并写入数据库。 2、java读取数据库数据,并导出为excel文件。 3、README.md中有详细的操作步骤示例。 使用说明: 1. 先使用postman导入:other/excel相关.postman_collection....
使用matlab对Excel文件进行操作,包括写入和读取,也就是实现Excel和Matlab交互
此源代码用C#语言创建了Excel文档,并进行了读取和写入操作。
C#编写代码,可以读取任何版本的Excel文件,并写入二维数组中。
尝试了一下:ASP读取Excel文件并写入Access数据库。测试成功。把程序拿出来共享一下。