保存DataGridView数据到excel
2009-09-07 16:25
316 查看
这个是导出数据的类。
特别说明:导出时需要有一个只有一行标题列名行的excel文件。
public class CExcelWrite
{
private OleDbConnection m_oleconn;
private OleDbCommand m_olecomm;
/// <summary>
/// 准备开始写excel文件
/// </summary>
/// <param name="colcount">被写Excel文件列数</param>
/// <param name="modfilename">模板文件路径</param>
/// <param name="destfilename">被写文件路径</param>
public void BeginWrite(int colcount,string modfilename,string destfilename)
{
if (System.IO.File.Exists(destfilename))
System.IO.File.Delete(destfilename);
System.IO.File.Copy(modfilename, destfilename);
string connstr = string.Format(
"Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=no';Data Source={0}",
destfilename);
string commstr = "";
string commstr1 = "";
m_oleconn = new OleDbConnection(connstr);
m_oleconn.Open();
m_olecomm=new OleDbCommand();
m_olecomm.Connection = m_oleconn;
commstr = "insert into [Sheet1$](";
commstr1 = "values(";
for (int i = 1; i <= colcount; i++)
{
commstr += "F" + i.ToString();
commstr1 += "?";
if (i < colcount)
{
commstr += ",";
commstr1 += ",";
}
}
commstr += ")";
commstr1 += ")";
m_olecomm.CommandText = commstr + commstr1;
for (int i = 1; i <= colcount; i++)
m_olecomm.Parameters.Add("@Col" + i.ToString(), OleDbType.VarChar, 250);
}
/// <summary>
/// 写入一行数据
/// </summary>
/// <param name="outdata"></param>
public bool WriteLine(string[] outdata)
{
if (m_oleconn!=null && outdata != null && outdata.Length > 0)
{
for (int i = 0; i < outdata.Length; i++)
{
m_olecomm.Parameters[i].Value = outdata[i];
}
m_olecomm.ExecuteNonQuery();
return true;
}
return false;
}
/// <summary>
/// 结束写入数据
/// </summary>
public void EndWrite()
{
if (m_oleconn != null)
{
m_olecomm.Dispose();
m_oleconn.Close();
m_oleconn = null;
m_olecomm = null;
}
}
~CExcelWrite()
{
EndWrite();
}
}
使用方法
private void btnoutexcel_Click(object sender, EventArgs e)
{
if (sFDExcel.ShowDialog() == DialogResult.OK)
{
if (dGVmszh.Rows.Count > 0)
{
string[] coldata = new string[7];
CExcelWrite cew = new CExcelWrite();
//初始化
cew.BeginWrite(7, Application.StartupPath + "//ModelFile.xls", sFDExcel.FileName);
foreach (DataGridViewRow dgvr in dGVmszh.Rows)
{
int j = 0;
for (int i = 1; i <= 3; i++,j++)
coldata[j] = dgvr.Cells[i].Value.ToString();
for (int i = 5; i <= 8; i++,j++)
coldata[j] = dgvr.Cells[i].Value.ToString();
//写入一行
cew.WriteLine(coldata);
}
//结束写入
cew.EndWrite();
}
}
}
特别说明:导出时需要有一个只有一行标题列名行的excel文件。
public class CExcelWrite
{
private OleDbConnection m_oleconn;
private OleDbCommand m_olecomm;
/// <summary>
/// 准备开始写excel文件
/// </summary>
/// <param name="colcount">被写Excel文件列数</param>
/// <param name="modfilename">模板文件路径</param>
/// <param name="destfilename">被写文件路径</param>
public void BeginWrite(int colcount,string modfilename,string destfilename)
{
if (System.IO.File.Exists(destfilename))
System.IO.File.Delete(destfilename);
System.IO.File.Copy(modfilename, destfilename);
string connstr = string.Format(
"Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=no';Data Source={0}",
destfilename);
string commstr = "";
string commstr1 = "";
m_oleconn = new OleDbConnection(connstr);
m_oleconn.Open();
m_olecomm=new OleDbCommand();
m_olecomm.Connection = m_oleconn;
commstr = "insert into [Sheet1$](";
commstr1 = "values(";
for (int i = 1; i <= colcount; i++)
{
commstr += "F" + i.ToString();
commstr1 += "?";
if (i < colcount)
{
commstr += ",";
commstr1 += ",";
}
}
commstr += ")";
commstr1 += ")";
m_olecomm.CommandText = commstr + commstr1;
for (int i = 1; i <= colcount; i++)
m_olecomm.Parameters.Add("@Col" + i.ToString(), OleDbType.VarChar, 250);
}
/// <summary>
/// 写入一行数据
/// </summary>
/// <param name="outdata"></param>
public bool WriteLine(string[] outdata)
{
if (m_oleconn!=null && outdata != null && outdata.Length > 0)
{
for (int i = 0; i < outdata.Length; i++)
{
m_olecomm.Parameters[i].Value = outdata[i];
}
m_olecomm.ExecuteNonQuery();
return true;
}
return false;
}
/// <summary>
/// 结束写入数据
/// </summary>
public void EndWrite()
{
if (m_oleconn != null)
{
m_olecomm.Dispose();
m_oleconn.Close();
m_oleconn = null;
m_olecomm = null;
}
}
~CExcelWrite()
{
EndWrite();
}
}
使用方法
private void btnoutexcel_Click(object sender, EventArgs e)
{
if (sFDExcel.ShowDialog() == DialogResult.OK)
{
if (dGVmszh.Rows.Count > 0)
{
string[] coldata = new string[7];
CExcelWrite cew = new CExcelWrite();
//初始化
cew.BeginWrite(7, Application.StartupPath + "//ModelFile.xls", sFDExcel.FileName);
foreach (DataGridViewRow dgvr in dGVmszh.Rows)
{
int j = 0;
for (int i = 1; i <= 3; i++,j++)
coldata[j] = dgvr.Cells[i].Value.ToString();
for (int i = 5; i <= 8; i++,j++)
coldata[j] = dgvr.Cells[i].Value.ToString();
//写入一行
cew.WriteLine(coldata);
}
//结束写入
cew.EndWrite();
}
}
}
相关文章推荐
- C#将DataGridView中的数据保存为EXCEL的方法
- C#将DataGridView中的数据保存为EXCEL的方法
- .NET编程技术——实验10:将DataGridView表单数据保存为excel文件,表单的基本格式化
- 【C#】将DataGridView数据保存到Excel中
- C#中把当前显示的datagridview中的数据以excel的形式导出
- 将Excel中数据导入DataGridView中并实现筛选功能
- c# winform DataGridView导出数据到Excel中,可以导出当前页和全部数据
- winfrom 如何保存datagridview中的某一行数据
- python数据如何保存到excel中
- DataGridview中的数据如何导出到Excel中
- 上传,解析Excel文件并保存数据到数据库
- 基于yaf框架和uploadify插件,做的一个导入excel文件,查看并保存数据的功能
- 怎样读取本地Excel数据,并保存到时服务器
- java中生成30万的excel(采用多个excel,每个上面放6万数据,最后打包zip保存)
- 利用poi把数据保存excel,并格式化日期实例
- C# datagridview导出数据至Excel
- DataGridview焦点不移开不保存数据问题
- c#winform中,对DataGridView数据进行操作,一次性保存
- 导出DataGridView显示列数据到Excel中:)
- 如何将Excel和xml文件数据保存到数据库