这篇文章介绍了C#将DataGridView中的数据保存到CSV和Excel中的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
一、保存到CSV
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;
}
二、保存到Excel
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的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持得得之家。
沃梦达教程
本文标题为:C#将DataGridView中的数据保存到CSV和Excel中
基础教程推荐
猜你喜欢
- unity实现动态排行榜 2023-04-27
- C# windows语音识别与朗读实例 2023-04-27
- C# List实现行转列的通用方案 2022-11-02
- winform把Office转成PDF文件 2023-06-14
- C#控制台实现飞行棋小游戏 2023-04-22
- C# 调用WebService的方法 2023-03-09
- 一个读写csv文件的C#类 2022-11-06
- C#类和结构详解 2023-05-30
- linux – 如何在Debian Jessie中安装dotnet core sdk 2023-09-26
- ZooKeeper的安装及部署教程 2023-01-22