C#DataGridView导入Excel数据,并上传数据

好久没来写文章了,今天和大家分享一个小操作!

C#DataGridView导入Excel数据,并上传数据_第1张图片

因项目需要需要将财务数据导入流程数据中。项目需要解决的几个问题:1、将数据导入和上传;2、由于财务用的系统导出的数据无法阅览难读大,对账不方便,因此需要对数据汇总并做转行,以及做合计运算。本次分享是数据导入和上传的主要语句。

//引用

using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;//关键
using BLL;  //上传数据接口
using Mods;// 对象


//调取数据

 private void ReadExcelData_Click(object sender, EventArgs e)
 {
       try
         {    
                OpenFileDialog FileOpen = new OpenFileDialog();
                if (FileOpen.ShowDialog() == DialogResult.OK)
                {

                    excelPath = FileOpen.FileName;
                    FileType = System.IO.Path.GetExtension(excelPath);
                }
                 FileType = System.IO.Path.GetExtension(excelPath);
                    if (FileType == ".xls")
                    {
                        sqlCon = @"Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = '" + excelPath + "';Extended Properties=Excel 8.0";
                    }// 旧版本2003
                    else
                    {
                        sqlCon = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelPath + ";Extended Properties='Excel 12.0;HDR=YES;IEMX=1'";
                    }
                    string ExcelStr = "select * from [Sheet1$]";
                    showExcelContent(sqlCon, ExcelStr);            
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

//显示数据和

  private void ShowExcelContent(string ECon, string Esqlstr)
   {
            try
            {
                DataSet ds = new DataSet();
                OleDbConnection oleConn = new OleDbConnection(ECon);
                OleDbDataAdapter oleData = new OleDbDataAdapter(Esqlstr, oleConn);
                oleConn.Open();
                oleData.Fill(ds, "Info");
                oleConn.Close();
                dst = ds;//dst用于上传数据,为什么需要这个,主要是在一张表中做多数据切换上传
                InputDataGridView.DataSource = ds.Tables[0];               
            }
            catch (Exception ex)
            {
                throw new Exception("显示数据发生错误:"+ex.Message);
            }

        }

//上传数据

 private void UpLoadData()
        {         
            SqlConnection sqlCon = new SqlConnection(M_str_sqlcon);
            SqlCommand sqlCom = new SqlCommand();
            sqlCom.Connection = sqlCon;
            sqlCon.Open();
            FinanceStoreData objFSD;        
            try
            {
                if (InputStoreData.Checked)
                {
                    DialogResult result = MessageBox.Show("即将保存“入库数据“”是否确定", "提醒!!", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                    if (result == DialogResult.OK)
                    {
                        for (int i = 0; i < dst.Tables[0].Rows.Count; i++)
                        {
                            if (dst.Tables[0].Rows[i].ItemArray[0].ToString().Trim() != "")
                            {
                                objFSD = new FinanceStoreData()
                                {
                                    FinanceNO = dst.Tables[0].Rows[i].ItemArray[0].ToString().Trim(),
                                    PartMentName = dst.Tables[0].Rows[i].ItemArray[1].ToString().Trim(),
                                    StoreProcessNo = dst.Tables[0].Rows[i].ItemArray[2].ToString().Trim(),

                                   .....
                                };
                                objFinanceStoreManager.FinaceStoreData(objFSD);//上传数据
                            }
                        }
                    }

                MessageBox.Show("上传成功!");
                }

         catch (Exception ex)
            {
                throw new Exception("上传数据发生错误:"+ex.Message);
            }

}

你可能感兴趣的:(编程,DataGridView,c#,开发语言,后端)