You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
5.8 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Windows.Forms;
namespace demo.ClassHelper.DgvOperate
{
class DgvHelper
{
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 void DgvToExcel(DataGridView dgv, string path)
//{
// string fileName = "";
// string saveFileName = "";
// SaveFileDialog saveDialog = new SaveFileDialog();
// saveDialog.InitialDirectory = path;
// if (!Directory.Exists(@saveDialog.InitialDirectory))
// {
// Directory.CreateDirectory(@saveDialog.InitialDirectory);
// }
// saveDialog.FileName = DateTime.Now.ToString("yyyyMMdd");
// saveDialog.DefaultExt = "xlsx";
// saveDialog.Filter = "Excel文件|*.xlsx";
// saveDialog.ShowDialog();
// saveFileName = saveDialog.FileName;
// if (saveFileName.IndexOf(":") < 0) return; //被点了取消
// Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
// if (xlApp == null)
// {
// MessageBox.Show("无法创建Excel对象您的电脑可能未安装Excel");
// return;
// }
// 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 < dgv.ColumnCount; i++)
// { worksheet.Cells[1, i + 1] = dgv.Columns[i].HeaderText; }
// //写入数值
// for (int r = 0; r < dgv.Rows.Count; r++)
// {
// for (int i = 0; i < dgv.ColumnCount; i++)
// {
// worksheet.Cells[r + 2, i + 1] = dgv.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();
// GC.Collect();//强行销毁
//}
}
}