欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#將DataGridView中的數(shù)據(jù)保存到CSV和Excel中

 更新時(shí)間:2022年04月27日 11:34:35   作者:農(nóng)碼一生  
這篇文章介紹了C#將DataGridView中的數(shù)據(jù)保存到CSV和Excel中的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

一、保存到CSV

        public static bool dataGridViewToCSV(DataGridView dataGridView)
        {
            if (dataGridView.Rows.Count == 0)
            {
                MessageBox.Show("沒有數(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 + "\""; //每個(gè)元素值用引號(hào)包括
                                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;
        }

二、保存到Excel

        public static bool dataGridViewToExcel(DataGridView dataGridView)
        {
            if (dataGridView.Rows.Count == 0)
            {
                MessageBox.Show("沒有數(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("無法創(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 
                                                                                         //寫入標(biāo)題             
            for (int i = 0; i < dataGridView.ColumnCount; i++)
            { worksheet.Cells[1, i + 1] = dataGridView.Columns[i].HeaderText; }
            //寫入數(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ò),文件可能正被打開!\n" + ex.Message);
                }
            }
            xlApp.Quit();
            Kill(xlApp);
            GC.Collect();//強(qiáng)行銷毀      
            return true;
        }

到此這篇關(guān)于C#將DataGridView數(shù)據(jù)保存CSV和Excel的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#裝箱和拆箱操作實(shí)例分析

    C#裝箱和拆箱操作實(shí)例分析

    這篇文章主要介紹了C#裝箱和拆箱操作,結(jié)合實(shí)例形式分析了C#中裝箱與拆箱的概念、用法及相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2016-08-08
  • C# cmd中修改顯示(顯示進(jìn)度變化效果)的方法

    C# cmd中修改顯示(顯示進(jìn)度變化效果)的方法

    好多人想在運(yùn)行或者調(diào)試含有大量數(shù)據(jù)或者比較慢C#程序的時(shí)候能夠顯示自己的程序完成的程度,這里有一個(gè)方法能發(fā)不斷地修改cmd的同一行,以達(dá)到顯示完成百分比的目的
    2013-04-04
  • C#最簡單的關(guān)閉子窗體更新父窗體的實(shí)現(xiàn)方法

    C#最簡單的關(guān)閉子窗體更新父窗體的實(shí)現(xiàn)方法

    原理就是將子窗體最為對(duì)話框模式彈出,當(dāng)窗體關(guān)閉或取消時(shí)更新主窗體
    2012-11-11
  • c#獲取圖片正確格式的方法

    c#獲取圖片正確格式的方法

    這篇文章主要介紹了c#獲取圖片正確格式的方法,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • C# 多網(wǎng)卡 Server Listen

    C# 多網(wǎng)卡 Server Listen

    C# 多網(wǎng)卡 Server Listen...
    2007-04-04
  • 打開一個(gè)Unity工程步驟

    打開一個(gè)Unity工程步驟

    這篇文章講述了如何打開一個(gè)Unity工程,包含詳細(xì)的圖文介紹的步驟,希望本文對(duì)你有所幫助
    2021-06-06
  • C#?wpf?Bitmap轉(zhuǎn)換成WriteableBitmap的方法

    C#?wpf?Bitmap轉(zhuǎn)換成WriteableBitmap的方法

    本文主要介紹了C#?wpf?Bitmap轉(zhuǎn)換成WriteableBitmap的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • c#分頁顯示服務(wù)器上指定目錄下的所有圖片示例

    c#分頁顯示服務(wù)器上指定目錄下的所有圖片示例

    這篇文章主要介紹了c#分頁顯示服務(wù)器上指定目錄下的所有圖片示例,需要的朋友可以參考下
    2014-05-05
  • 利用C#實(shí)現(xiàn)合并Word文檔功能

    利用C#實(shí)現(xiàn)合并Word文檔功能

    合并Word文檔可以快速地將多份編輯好的文檔合在一起,避免復(fù)制粘貼時(shí)遺漏內(nèi)容,以及耗費(fèi)不必要的時(shí)間。本文將分為以下兩部分介紹如何通過C#合并Word文檔,并附上VB.NET代碼供大家參考,希望對(duì)大家有所幫助
    2022-12-12
  • C# 讀寫XML(代碼分享)

    C# 讀寫XML(代碼分享)

    本文主要介紹了C# 讀寫XML的相關(guān)知識(shí),具有很好的參考價(jià)值。下面跟著小編一起來看下吧
    2017-03-03

最新評(píng)論