C#將DataGridView中的數(shù)據(jù)保存到CSV和Excel中
更新時間:2022年04月27日 11:34:35 作者:農(nóng)碼一生
這篇文章介紹了C#將DataGridView中的數(shù)據(jù)保存到CSV和Excel中的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
一、保存到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(); //防止里面含有特殊符號 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)出錯誤", 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; //被點了取消 Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); if (xlApp == null) { MessageBox.Show("無法創(chuàng)建Excel對象,您的電腦可能未安裝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)出文件時出錯,文件可能正被打開!\n" + ex.Message); } } xlApp.Quit(); Kill(xlApp); GC.Collect();//強行銷毀 return true; }
到此這篇關(guān)于C#將DataGridView數(shù)據(jù)保存CSV和Excel的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#最簡單的關(guān)閉子窗體更新父窗體的實現(xiàn)方法
原理就是將子窗體最為對話框模式彈出,當(dāng)窗體關(guān)閉或取消時更新主窗體2012-11-11C#?wpf?Bitmap轉(zhuǎn)換成WriteableBitmap的方法
本文主要介紹了C#?wpf?Bitmap轉(zhuǎn)換成WriteableBitmap的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08