using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
namespace AssetsManage.Operation
{
class Export
{
private static System.Windows.Forms.DataGridView gridView;
private static System.Windows.Forms.ToolStripProgressBar toolStripProgressBar1;
private static Timer time;
private static DataSet objSet = new DataSet();
private static SaveFileDialog saveFileDialog = new SaveFileDialog();
private static SaveFileDialog saveFileDialog2 = new SaveFileDialog();
public static System.Windows.Forms.DataGridView _gridView
{
get { return gridView; }
set { gridView = value;}
}
public static System.Windows.Forms.ToolStripProgressBar _toolStripProgressBar1
{
get { return toolStripProgressBar1; }
set { toolStripProgressBar1 = value;}
}
public static Timer _time
{
get { return time; }
set { time = value;}
}
public static DataSet _objSet
{
get { return objSet; }
set { objSet.Clear(); objSet = value; }
}
//导出当前页DataGridView中的数据到EXcel中
public static void ExportTOExcel()
{
if (gridView.Rows.Count == 0)
{
MessageBox.Show("没有数据可供导出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.CreatePrompt = true;
saveFileDialog.Title = "导出文件保存路径";
saveFileDialog.ShowDialog();
string strName = saveFileDialog.FileName;
if (strName.Length != 0)
{
toolStripProgressBar1.Visible = true;
System.Reflection.Missing miss = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
excel.Application.Workbooks.Add(true); ;
excel.Visible = false;//若是true,则在导出的时候会显示EXcel界面。
if (excel == null)
{
MessageBox.Show("EXCEL无法启动!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
Microsoft.Office.Interop.Excel.Workbooks books = (Microsoft.Office.Interop.Excel.Workbooks)excel.Workbooks;
Microsoft.Office.Interop.Excel.Workbook book = (Microsoft.Office.Interop.Excel.Workbook)(books.Add(miss));
Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)book.ActiveSheet;
sheet.Name = "test";
//生成字段名称
for (int i = 0; i < gridView.ColumnCount; i++)
{
excel.Cells[1, i + 1] = gridView.Columns[i].HeaderText.ToString();
}
//填充数据
for (int i = 0; i < gridView.RowCount - 1; i++)
{
for (int j = 0; j < gridView.ColumnCount; j++)
{
if (gridView[j, i].Value == typeof(string))
{
excel.Cells[i + 2, j + 1] = "" + gridView[i, j].Value.ToString();
}
else
{
excel.Cells[i + 2, j + 1] = gridView[j, i].Value.ToString();
}
}
toolStripProgressBar1.Value += 100 / gridView.RowCount;
}
sheet.SaveAs(strName, miss, miss, miss, miss, miss, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss);
book.Close(false, miss, miss);
books.Close();
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
System.Runtime.InteropServices.Marshal.ReleaseComObject(books);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
GC.Collect();
MessageBox.Show("数据已经成功导出到:" + saveFileDialog.FileName.ToString(), "导出完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
toolStripProgressBar1.Value = 0;
toolStripProgressBar1.Visible = false;
}
}
}
//-------------------------------------------------------------------------------------------------------------------------------------
//导出整个DataGridView中的数据到Excel中
public static void ExportTOExcel2()
{
if (gridView.Rows.Count == 0)
{
MessageBox.Show("没有数据可供导出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
saveFileDialog2.Filter = "Execl files (*.xls)|*.xls";
saveFileDialog2.FilterIndex = 0;
saveFileDialog2.RestoreDirectory = true;
//saveFileDialog2.CreatePrompt = true;
saveFileDialog2.Title = "导出文件保存路径";
saveFileDialog2.FileName = null;
saveFileDialog2.ShowDialog();
string FileName = saveFileDialog2.FileName;
if (FileName.Length != 0)
{
toolStripProgressBar1.Visible = true;
System.Data.DataTable dt = objSet.Tables[0];
FileStream objFileStream;
StreamWriter objStreamWriter;
string strLine = "";
objFileStream = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Write);
objStreamWriter = new StreamWriter(objFileStream, System.Text.Encoding.Unicode);
toolStripProgressBar1.Value = 0;
for (int i = 0; i < dt.Columns.Count; i++)
{
strLine = strLine + dt.Columns[i].ColumnName.ToString() + Convert.ToChar(9);
}
objStreamWriter.WriteLine(strLine);
strLine = "";
for (int i = 0; i < dt.Rows.Count; i++)
{
strLine = strLine + (i + 1) + Convert.ToChar(9);
for (int j = 1; j < dt.Columns.Count; j++)
{
strLine = strLine + dt.Rows[i][j].ToString() + Convert.ToChar(9);
}
objStreamWriter.WriteLine(strLine);
toolStripProgressBar1.Value += 100 / dt.Rows.Count;
strLine = "";
}
objStreamWriter.Close();
objFileStream.Close();
MessageBox.Show("数据已经成功导出到:" + saveFileDialog2.FileName.ToString(), "导出完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
toolStripProgressBar1.Value = 0;
toolStripProgressBar1.Visible = false;
c# 导出到Excel (C# winform)
4星 · 超过85%的资源 需积分: 46 160 浏览量
2010-02-09
14:52:00
上传
评论 2
收藏 2KB RAR 举报
txq_hd
- 粉丝: 2
- 资源: 14
最新资源
- libjpeg 编译所需的 Win32.mak vs编译libjpeg
- 自动驾驶-状态估计和定位-粒子滤波实现和源码.pdf
- 数据可视化-智慧物流服务中心大屏页面.zip
- yolov5,SSD 可能使用到的一些代码
- bbbbbbbbbbbbbbbbbb
- 安卓逆向学习笔记之Frida Stalker 还原OLLVM AES.docx
- 安卓逆向学习笔记之unicorn来trace还原OLLVM Base64.docx
- 基于jquery的自定义表格组件实现
- Nessus最新20240426离线安装插件all-2.0.tar.gz
- 最新版本私钥助记词碰撞器大富豪使用python进行制作通过接口的方式进行验证支持多币种多链多网络一分钟万次验证高出货率
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
前往页