C#操作Excel(导入导出) (转帖)

C#操作Excel(导入导出)

转载地址:http://www.pconline.com.cn/pcedu/empolder/net/cs/0507/674430.html

 

有很多朋友说需要C#导出到Excel的代码,现共享给大家

///  
///  读取Excel文档
///  
///  文件名称
///  返回一个数据集
public  DataSet ExcelToDS( string  Path)
{
string  strConn  =   " Provider=Microsoft.Jet.OLEDB.4.0; "   + " Data Source= " +  Path  + " ; " + " Extended Properties=Excel 8.0; " ;
OleDbConnection conn 
=   new  OleDbConnection(strConn);
conn.Open(); 
string  strExcel  =   ""
OleDbDataAdapter myCommand 
=   null ;
DataSet ds 
=   null ;
strExcel
= " select * from [sheet1$] " ;
myCommand 
=   new  OleDbDataAdapter(strExcel, strConn);
ds 
=   new  DataSet();
myCommand.Fill(ds,
" table1 " ); 
return  ds;
}


///  
///  写入Excel文档
///  
///  文件名称
public   bool  SaveFP2toExcel( string  Path)
{
try
{
string  strConn  =   " Provider=Microsoft.Jet.OLEDB.4.0; "   + " Data Source= " +  Path  + " ; " + " Extended Properties=Excel 8.0; " ;
OleDbConnection conn 
=   new  OleDbConnection(strConn);
conn.Open(); 
System.Data.OleDb.OleDbCommand cmd
= new  OleDbCommand ();
cmd.Connection 
= conn;
// cmd.CommandText ="UPDATE [sheet1$] SET 姓名='2005-01-01' WHERE 工号='日期'";
// cmd.ExecuteNonQuery ();
for ( int  i = 0 ;i {
if (fp2.Sheets [ 0 ].Cells[i, 0 ].Text != "" )
{
cmd.CommandText 
= " INSERT INTO [sheet1$] (工号,姓名,部门,职务,日期,时间) VALUES(' " + fp2.Sheets [ 0 ].Cells[i, 0 ].Text +   " ',' " +
fp2.Sheets [
0 ].Cells[i, 1 ].Text + " ',' " + fp2.Sheets [ 0 ].Cells[i, 2 ].Text + " ',' " + fp2.Sheets [ 0 ].Cells[i, 3 ].Text +
" ',' " + fp2.Sheets [ 0 ].Cells[i, 4 ].Text + " ',' " + fp2.Sheets [ 0 ].Cells[i, 5 ].Text + " ') " ;
cmd.ExecuteNonQuery ();
}
}
conn.Close ();
return   true ;
}
catch (System.Data.OleDb.OleDbException ex)
{
System.Diagnostics.Debug.WriteLine (
" 写入Excel发生错误: " + ex.Message );
}
return   false ;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
C# 中,您可以使用以下代码实现 DataGridView 数据的导入导出 Excel 文件的功能。 导入 Excel 文件到 DataGridView: ```csharp using System; using System.Data; using System.Windows.Forms; using Excel = Microsoft.Office.Interop.Excel; public void ImportExcelToDataGridView(DataGridView dataGridView, string filePath) { Excel.Application excelApp = new Excel.Application(); Excel.Workbook workbook = excelApp.Workbooks.Open(filePath); Excel.Worksheet worksheet = workbook.ActiveSheet; int rowCount = worksheet.UsedRange.Rows.Count; int columnCount = worksheet.UsedRange.Columns.Count; DataTable dataTable = new DataTable(); for (int col = 1; col <= columnCount; col++) { dataTable.Columns.Add(worksheet.Cells[1, col].Value.ToString()); } for (int row = 2; row <= rowCount; row++) { DataRow dataRow = dataTable.NewRow(); for (int col = 1; col <= columnCount; col++) { dataRow[col - 1] = worksheet.Cells[row, col].Value; } dataTable.Rows.Add(dataRow); } dataGridView.DataSource = dataTable; workbook.Close(); excelApp.Quit(); } ``` 导出 DataGridView 数据到 Excel 文件: ```csharp using System; using System.Data; using System.Windows.Forms; using Excel = Microsoft.Office.Interop.Excel; public void ExportDataGridViewToExcel(DataGridView dataGridView, string filePath) { Excel.Application excelApp = new Excel.Application(); Excel.Workbook workbook = excelApp.Workbooks.Add(); Excel.Worksheet worksheet = workbook.ActiveSheet; for (int col = 1; col <= dataGridView.Columns.Count; col++) { worksheet.Cells[1, col] = dataGridView.Columns[col - 1].HeaderText; worksheet.Cells[1, col].Font.Bold = true; } for (int row = 0; row < dataGridView.Rows.Count; row++) { for (int col = 0; col < dataGridView.Columns.Count; col++) { worksheet.Cells[row + 2, col + 1] = dataGridView.Rows[row].Cells[col].Value.ToString(); } } worksheet.Columns.AutoFit(); workbook.SaveAs(filePath); workbook.Close(); excelApp.Quit(); } ``` 您可以调用 `ImportExcelToDataGridView` 方法将 Excel 文件导入到 DataGridView,传入要导入的 DataGridView 对象和 Excel 文件路径。调用 `ExportDataGridViewToExcel` 方法将 DataGridView 数据导出Excel 文件,传入要导出的 DataGridView 对象和 Excel 文件路径。 请注意,导入导出 Excel 文件需要在项目中引用 Microsoft.Office.Interop.Excel 库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值