一、保存到CSV

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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

安装 Office/SharePoint 开发

正常调用的话会出现 没有 Microsoft.Office.XXX 错误

1、前言

今天在写 C# 实验的时候遇到了

  • 命名空间“Microsoft”中不存在类型或命名空间名“Office”(是否缺少程序集引用?)

  • “Microsoft.Office.Interop”中不存在类型或命名空间名“Word”(是否缺少程序集引用?)
    等等问题,关于 Office 的引用的,起初我查找网上的各种方法,发现都没有用,包括手动选择导入电脑中的 dll 和安装 NuGet 程序包,结果都是 “warning MSB3245: 未能解析此引用”

后来在偶然中发现在 Visual Studio Installer 中安装 Office/SharePoint 开发 可以解决这个问题。

2、解决方案

打开 Visual Studio 2019,选择 工具->获取工具和功能

弹出如下界面,勾选 Office/SharePoint 开发 并点击修改

等待安装完成,然后重启 VS2019,就可以在引用管理器里看到 Microsoft.Office.XXX 等包了,并且编译也不会出现其他版本问题了。

实现代码

Kill(xlApp); 这个没有实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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;
}

相关链接(侵删)

  1. C#将DataGridView中的数据保存到CSV和Excel中
  2. 【C#】VS2019 添加引用中没有 Microsoft.Office.XXX 的解决办法

=================我是分割线=================

欢迎到公众号来唠嗑: