C#如何將DataGridView中的數(shù)據(jù)導(dǎo)入到Csv文件及導(dǎo)出到Excel

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)C#如何將DataGridView中的數(shù)據(jù)導(dǎo)入到Csv文件及導(dǎo)出到Excel,文章內(nèi)容豐富且以專(zhuān)業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

10年的金門(mén)網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開(kāi)發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。營(yíng)銷(xiāo)型網(wǎng)站的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整金門(mén)建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無(wú)論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)建站從事“金門(mén)網(wǎng)站設(shè)計(jì)”,“金門(mén)網(wǎng)站推廣”以來(lái),每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。

 1,將DataGridView中的數(shù)據(jù)導(dǎo)入到Csv文件中

 public static bool dataGridViewToCSV(DataGridView dataGridView)
        {
            if (dataGridView.Rows.Count == 0)
            {
                MessageBox.Show("沒(méi)有數(shù)據(jù)可導(dǎo)出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "CSV files (*.csv)|*.csv";
            saveFileDialog.FilterIndex = 0;
            saveFileDialog.RestoreDirectory = true;
            saveFileDialog.CreatePrompt = true;
            saveFileDialog.FileName = null;
            saveFileDialog.Title = "保存";
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                Stream stream = saveFileDialog.OpenFile();
                StreamWriter sw = new StreamWriter(stream, System.Text.Encoding.GetEncoding(-0));
                string strLine = "";
                try
                {
                    //表頭
                    for (int i = 0; i < dataGridView.ColumnCount; i++)
                    {
                        if (i > 0)
                            strLine += ",";
                        strLine += dataGridView.Columns[i].HeaderText;
                    }
                    strLine.Remove(strLine.Length - 1);
                    sw.WriteLine(strLine);
                    strLine = "";
                    //表的內(nèi)容
                    for (int j = 0; j < dataGridView.Rows.Count; j++)
                    {
                        strLine = "";
                        int colCount = dataGridView.Columns.Count;
                        for (int k = 0; k < colCount; k++)
                        {
                            if (k > 0 && k < colCount)
                                strLine += ",";
                            if (dataGridView.Rows[j].Cells[k].Value == null)
                                strLine += "";
                            else
                            {
                                string cell = dataGridView.Rows[j].Cells[k].Value.ToString().Trim();
                                //防止里面含有特殊符號(hào)
                                cell = cell.Replace("\"", "\"\"");
                                cell = "\"" + cell + "\"";
                                strLine += cell;
                            }
                        }
                        sw.WriteLine(strLine);
                    }
                    sw.Close();
                    stream.Close();
                    MessageBox.Show("數(shù)據(jù)被導(dǎo)出到:" + saveFileDialog.FileName.ToString(), "導(dǎo)出完畢", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "導(dǎo)出錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
            }
            return true;
        }

2,C#中將DataGridView中的數(shù)據(jù)導(dǎo)出到Excel中

public static bool dataGridViewToExcel(DataGridView dataGridView)
        {
            if (dataGridView.Rows.Count == 0)
            {
                MessageBox.Show("沒(méi)有數(shù)據(jù)可導(dǎo)出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            string fileName = "";
            string saveFileName = "";
            SaveFileDialog saveDialog = new SaveFileDialog();
            saveDialog.DefaultExt = "xlsx";
            saveDialog.Filter = "Excel文件|*.xlsx";
            saveDialog.FileName = fileName;
            saveDialog.ShowDialog();
            saveFileName = saveDialog.FileName;
            if (saveFileName.IndexOf(":") < 0)
                return false; //被點(diǎn)了取消
            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            if (xlApp == null)
            {
                MessageBox.Show("無(wú)法創(chuàng)建Excel對(duì)象,您的電腦可能未安裝Excel");
                return false;
            }
            Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
            Microsoft.Office.Interop.Excel.Workbook workbook =
                        workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
            Microsoft.Office.Interop.Excel.Worksheet worksheet =
                        (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1 
                                                                                         //寫(xiě)入標(biāo)題             
            for (int i = 0; i < dataGridView.ColumnCount; i++)
            { worksheet.Cells[1, i + 1] = dataGridView.Columns[i].HeaderText; }
            //寫(xiě)入數(shù)值
            for (int r = 0; r < dataGridView.Rows.Count; r++)
            {
                for (int i = 0; i < dataGridView.ColumnCount; i++)
                {
                    worksheet.Cells[r + 2, i + 1] = dataGridView.Rows[r].Cells[i].Value;
                }
                System.Windows.Forms.Application.DoEvents();
            }
            worksheet.Columns.EntireColumn.AutoFit();//列寬自適應(yīng)
            MessageBox.Show(fileName + "資料保存成功", "提示", MessageBoxButtons.OK);
            if (saveFileName != "")
            {
                try
                {
                    workbook.Saved = true;
                    workbook.SaveCopyAs(saveFileName);  //fileSaved = true;                 
                }
                catch (Exception ex)
                {//fileSaved = false;                      
                    MessageBox.Show("導(dǎo)出文件時(shí)出錯(cuò),文件可能正被打開(kāi)!\n" + ex.Message);
                }
            }
            xlApp.Quit();
            Kill(xlApp);
            GC.Collect();//強(qiáng)行銷(xiāo)毀      
            return true;
        }
//*********************************************************************************************//
        //*功能:當(dāng)使用Excel.Application方法的時(shí)候,系統(tǒng)會(huì)自動(dòng)創(chuàng)建一個(gè)Excel進(jìn)程,即使當(dāng)你使用Excel.Quit()之后,也是不會(huì)關(guān)閉的。        //*參數(shù):通過(guò)進(jìn)程ID來(lái)唯一標(biāo)識(shí)我們自己創(chuàng)建的進(jìn)程,然后在關(guān)閉Excel.Application的時(shí)候一同將進(jìn)程Kill掉。        //*返回值:無(wú)        //*時(shí)間:2018-04-25        //**********************************************************************************************//        public static void Kill(Microsoft.Office.Interop.Excel.Application excel)        {            IntPtr t = new IntPtr(excel.Hwnd);            int k = 0;            GetWindowThreadProcessId(t, out k);            System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);            p.Kill();        }

上述就是小編為大家分享的C#如何將DataGridView中的數(shù)據(jù)導(dǎo)入到Csv文件及導(dǎo)出到Excel了,如果剛好有類(lèi)似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

本文名稱(chēng):C#如何將DataGridView中的數(shù)據(jù)導(dǎo)入到Csv文件及導(dǎo)出到Excel
鏈接URL:http://muchs.cn/article44/gjchee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、云服務(wù)器、標(biāo)簽優(yōu)化、動(dòng)態(tài)網(wǎng)站網(wǎng)站收錄、自適應(yīng)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名