CSV到GridView比CRUD操作

时间:2015-04-03 16:36:37

标签: c# winforms csv gridview

我正在制作一个Job Report应用程序,直到现在我所做的是,将CSV文件导入到网格视图并通过将其保存在数据表中来显示它,现在我想要的是,更新并将记录保存回来csv文件,我没有使用任何SQL或任何类型的数据库,是否可以这样做? 请帮助我,我必须在两个小时内交付项目。 该项目是C#Win表单。 还帮助我如何序列化它上传到ftp服务器。 代码在这里......         private void openProjectToolStripMenuItem_Click_1(object sender,EventArgs e)         {

        // int size = -1;
        OpenFileDialog ofd = new OpenFileDialog()
        {
            Title = "Choose a File",
            InitialDirectory = @"c:\dev\",
            Filter = "Text Files (.txt)|*.txt|XML Files|*.xml|Word Documents (.docx)|*.docx",
            RestoreDirectory = true,
            Multiselect = false
        };



        if (ofd.ShowDialog() != System.Windows.Forms.DialogResult.OK)
        {
            MessageBox.Show("No file selected!");
            return;
        }
        using (StreamReader oStreamReader = new StreamReader(ofd.FileName))
        {
            try
            {
                Application.DoEvents();
                DataSet ds = new DataSet("ApplicationData");

                //some updates in the Datatable
                DataTable JobHeaderDataTable = new DataTable("JobHeaderDataTable");
                DataTable JobDate = new DataTable("JobDate");
                DataTable JobDateItems = new DataTable("JobDateItems");

                ds.Tables.Add(JobHeaderDataTable);
                ds.Tables.Add(JobDate);
                ds.Tables.Add(JobDateItems);

                int rowCount = 0;

                string[] columnNames = null;
                string[] oStreamDataValues = null;

                while (!oStreamReader.EndOfStream)
                {
                    string oStreamRowData = oStreamReader.ReadLine().Trim();
                    if (oStreamRowData.Length > 0)
                    {

                        oStreamDataValues = oStreamRowData.Split('-');

                        if (rowCount == 0 && oStreamDataValues[0].ToString() == "HDR")

                        {
                            rowCount = 1;
                            columnNames = oStreamDataValues;

                            for (int i = 1; i < columnNames.Length; i++)
                            {
                                DataColumn oDataColumn = new DataColumn(columnNames[i].ToUpper(), typeof(string));
                                oDataColumn.DefaultValue = string.Empty;
                                JobHeaderDataTable.Columns.Add(oDataColumn);
                            }

                            //// For Slider
                            //txtCompany.Text = oStreamDataValues.GetValue(1).ToString();
                            //txtLocation.Text = oStreamDataValues.GetValue(2).ToString();
                            //txtRigName.Text = oStreamDataValues.GetValue(3).ToString();
                            //txtState.Text = oStreamDataValues.GetValue(4).ToString();
                            //txtCounty.Text = oStreamDataValues.GetValue(5).ToString(); 
                            //txtWellName.Text = oStreamDataValues.GetValue(6).ToString();
                            //txtTownship.Text  = oStreamDataValues.GetValue(7).ToString();
                            //txtDescription.Text = oStreamDataValues.GetValue(8).ToString();
                            //txtBentHstSub.Text = oStreamDataValues.GetValue(9).ToString();
                            //txtBilToBend.Text = oStreamDataValues.GetValue(10).ToString();
                            //txtPadOD.Text = oStreamDataValues.GetValue(11).ToString();
                            //txtNBStab.Text = oStreamDataValues.GetValue(11).ToString();
                            //txtJob_ID.Text = oStreamDataValues.GetValue(12).ToString();

                            //// For Header

                            //txtCompanyHeader.Text = oStreamDataValues.GetValue(1).ToString();
                            //txtLocationHeader.Text = oStreamDataValues.GetValue(2).ToString();
                            //txtRigNameHeader.Text = oStreamDataValues.GetValue(3).ToString();
                            //txtStateHeader.Text = oStreamDataValues.GetValue(4).ToString();
                            //txtCountyHeader.Text = oStreamDataValues.GetValue(5).ToString();
                            //txtWellNameHeader.Text = oStreamDataValues.GetValue(6).ToString();
                            //txtTownshipHeader.Text = oStreamDataValues.GetValue(7).ToString();
                            //txtDescriptionHeader.Text = oStreamDataValues.GetValue(8).ToString();
                            //txtBentHstSubHeader.Text = oStreamDataValues.GetValue(9).ToString();
                            //txtBillToBendHeader.Text = oStreamDataValues.GetValue(10).ToString();
                            //txtPadODHeader.Text = oStreamDataValues.GetValue(11).ToString();
                            //txtNBStabHeader.Text = oStreamDataValues.GetValue(11).ToString();
                            //txtJob_IDHeader.Text = oStreamDataValues.GetValue(12).ToString();

                        }


                        else
                        {
                            DataRow oDataRow = JobHeaderDataTable.NewRow();

                            for (int i = 1; i < columnNames.Length; i++)
                            {
                                oDataRow[columnNames[i]] = oStreamDataValues[i] == null ? string.Empty : oStreamDataValues[i].ToString();

                            }

                            JobHeaderDataTable.Rows.Add(oDataRow);
                        }
                    }
                }

                oStreamReader.Close();
                oStreamReader.Dispose();

                foreach (DataRow dr in JobHeaderDataTable.Rows)
                {
                    dataGridView2.DataSource = JobHeaderDataTable;
                    dataGridView4.DataSource = JobDate;
                    dataGridView5.DataSource = JobDateItems;
                }
            }
            catch (IOException)
            {

            }

        }
    }

1 个答案:

答案 0 :(得分:0)

让自己保持一些头痛并查看ClosedXML,并使用此答案创建csv Can I save an EXCEL worksheet as CSV via ClosedXML?

    System.IO.File.WriteAllLines(csvFileName,
    worksheet.RowsUsed().Select(row =>
        string.Join(";", row.Cells(1, row.LastCellUsed(false).Address.ColumnNumber)
                            .Select(cell => cell.GetValue<string>()))
 ));