问下各位朋友
1.当程序读写Excel文件时,如果用户此时打开Excel文件,程序会崩溃。应该怎么样防止这样的问题啊?
2.C#读取Excel速度方面的优化主要考虑哪些方面,用数据库的方式读取Excel文件会快很多,这种方式和一行行遍历Excel读取有什么不同啊?
1.当程序读写Excel文件时,如果用户此时打开Excel文件,程序会崩溃。应该怎么样防止这样的问题啊?
2.C#读取Excel速度方面的优化主要考虑哪些方面,用数据库的方式读取Excel文件会快很多,这种方式和一行行遍历Excel读取有什么不同啊?
解决方案 »
- wpf中scrollviewer的模板的一个奇怪问题。为何scrollviewer是透明transparent的,仍然遮挡后面的控件(点击不了下面控件)?急急急
- System.Timers.Timer怎么一次比一次运行的速度快?
- 模仿Vista侧边栏CPU仪表盘控件能否改成webform?
- 串口通讯类serialport的使用问题
- 请问有什么asp.net的好书吗。
- C#构造函数和析构函数
- 如何解决关闭主窗体而不关闭整个程序的问题?(我是一个刚学习C#的莱鸟,大家不笑,帮帮忙吧)
- 如何将文件中的内容变成乱码?(在线等)
- 求C#电子书
- 一个关于DataGrid输出格式的问题
- vs新建项目时不能选择路径和解决方案了
- 关于默认浏览器的问题。。。
注意EXCEL多sheet,大数据量,分多sheet读取
sqlbuckcopy导入
正好写了个小程序,能解决你的问题,请参考:
源码:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Text;
using System.Windows.Forms; namespace btnnum
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} //获得的excel文件的名称
private string xlsname = string.Empty;
//定义DataSet,xls文件将填充到DataSet中
DataSet ds = new DataSet();
private void Form1_Load(object sender, EventArgs e)
{ } private void toolStripLabel1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Excel文件(*.xls;*.xlsx)|*.xls;*.xlsx";
ofd.Multiselect = false;
if (ofd.ShowDialog() == DialogResult.OK)
{
xlsname = ofd.FileName;
toolStripComboBox1.Enabled = true;
toolStripStatusLabel1.Text = "已经选择Excel文件: "+xlsname;
toolStripComboBox1.Items.Clear();
using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + xlsname + ";Extended Properties='Excel 12.0;HDR=NO;IMEX=1';"))
{
conn.Open();
DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
foreach (DataRow dr in dt.Rows)
{
toolStripComboBox1.Items.Add(dr["TABLE_NAME"]);
}
}
}
else
{
if (xlsname.Length > 0)
toolStripLabel2.Enabled = true;
else
toolStripLabel2.Enabled = false;
}
} private void toolStripLabel2_Click(object sender, EventArgs e)
{
try
{
ds.Clear();
using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + xlsname + ";Extended Properties='Excel 12.0;HDR=NO;IMEX=1'"))
{
using (OleDbDataAdapter da = new OleDbDataAdapter("Select * from ["+toolStripComboBox1.SelectedText+"]", conn))
{
da.Fill(ds, "xlssheet");
}
}
dataGridView1.DataSource = ds.Tables["xlssheet"];
dataGridView1.AutoGenerateColumns = true;
toolStripLabel3.Enabled = true;
}
catch
{
MessageBox.Show("打开EXCEL文件遇到错误,请检查是否合法的excel文件");
}
} private void toolStripLabel3_Click(object sender, EventArgs e)
{
richTextBox1.Text = "该EXCEL工作表的行列数信息:\n"+"共有: "+dataGridView1.Rows.Count+"行\n共有: "+dataGridView1.Columns.Count+"列";
} private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (toolStripComboBox1.SelectedIndex > -1)
{
toolStripLabel2.Enabled = true;
}
else
{
toolStripLabel2.Enabled = false;
}
}
}
}
源文件下载:
http://download.csdn.net/source/3034170
个人感觉这个小程序稳定性和效率还不错。其中读取EXCEL文件就是分Sheet读的,效率较高。请参考。如有不足请一定提出来,学习+完善,谢谢啊呵呵。