您好,登錄后才能下訂單哦!
這篇文章主要介紹“C#怎么將DataGridView中的數據保存到CSV和Excel中”,在日常操作中,相信很多人在C#怎么將DataGridView中的數據保存到CSV和Excel中問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”C#怎么將DataGridView中的數據保存到CSV和Excel中”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
public static bool dataGridViewToCSV(DataGridView dataGridView) { if (dataGridView.Rows.Count == 0) { MessageBox.Show("沒有數據可導出!", "提示", 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 = ""; //表的內容 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("數據被導出到:" + saveFileDialog.FileName.ToString(), "導出完畢", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show(ex.Message, "導出錯誤", MessageBoxButtons.OK, MessageBoxIcon.Information); return false; } } return true; }
public static bool dataGridViewToExcel(DataGridView dataGridView) { if (dataGridView.Rows.Count == 0) { MessageBox.Show("沒有數據可導出!", "提示", 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("無法創建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 //寫入標題 for (int i = 0; i < dataGridView.ColumnCount; i++) { worksheet.Cells[1, i + 1] = dataGridView.Columns[i].HeaderText; } //寫入數值 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();//列寬自適應 MessageBox.Show(fileName + "資料保存成功", "提示", MessageBoxButtons.OK); if (saveFileName != "") { try { workbook.Saved = true; workbook.SaveCopyAs(saveFileName); //fileSaved = true; } catch (Exception ex) {//fileSaved = false; MessageBox.Show("導出文件時出錯,文件可能正被打開!\n" + ex.Message); } } xlApp.Quit(); Kill(xlApp); GC.Collect();//強行銷毀 return true; }
到此,關于“C#怎么將DataGridView中的數據保存到CSV和Excel中”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。