您好,登錄后才能下訂單哦!
這期內(nèi)容當中小編將會給大家?guī)碛嘘P(guān)C#如何將DataGridView中的數(shù)據(jù)導入到Csv文件及導出到Excel,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
1,將DataGridView中的數(shù)據(jù)導入到Csv文件中
public static bool dataGridViewToCSV(DataGridView dataGridView) { if (dataGridView.Rows.Count == 0) { MessageBox.Show("沒有數(shù)據(jù)可導出!", "提示", 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ù)被導出到:" + saveFileDialog.FileName.ToString(), "導出完畢", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show(ex.Message, "導出錯誤", MessageBoxButtons.OK, MessageBoxIcon.Information); return false; } } return true; }
2,C#中將DataGridView中的數(shù)據(jù)導出到Excel中
public static bool dataGridViewToExcel(DataGridView dataGridView) { if (dataGridView.Rows.Count == 0) { MessageBox.Show("沒有數(shù)據(jù)可導出!", "提示", 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 //寫入標題 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("導出文件時出錯,文件可能正被打開!\n" + ex.Message); } } xlApp.Quit(); Kill(xlApp); GC.Collect();//強行銷毀 return true; }
//*********************************************************************************************// //*功能:當使用Excel.Application方法的時候,系統(tǒng)會自動創(chuàng)建一個Excel進程,即使當你使用Excel.Quit()之后,也是不會關(guān)閉的。 //*參數(shù):通過進程ID來唯一標識我們自己創(chuàng)建的進程,然后在關(guān)閉Excel.Application的時候一同將進程Kill掉。 //*返回值:無 //*時間: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ù)導入到Csv文件及導出到Excel了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。