资源简介
公司项目要求,做了一个C#程序,内容是excel导入导出+mysql数据库

代码片段和文件信息
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;
using System.Diagnostics;
using System.IO;
using Microsoft.Office.Interop.Excel;
namespace UIForm4
{
public class ExportDGVToExcel
{
private const int OLDOFFICEVESION = -4143;
private const int NEWOFFICEVESION = 56;
///
/// DataGridView导出Excel
///
/// Excel文件中的标题
/// DataGridView 控件
/// 0:成功;1:DataGridView中无记录;2:Excel无法启动;100:Cancel;9999:异常错误
public int ExportExcel(string strCaption DataGridView myDGV SaveFileDialog saveFileDialog)
{
//saveFileDialog.Filter = “Execl files (*.xls)|*.xls“;
saveFileDialog.Filter = “Excel files office2003(*.xls)|*.xls|Excel office2016(*.xlsx)|*.xlsx|All files (*.*)|*.*“;
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true;
//saveFileDialog.CreatePrompt = true;
saveFileDialog.title = “Export Excel File“;
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
if (saveFileDialog.FileName == ““)
{
MessageBox.Show(“请输入保存文件名!“);
saveFileDialog.ShowDialog();
}
// 列索引,行索引,总列数,总行数
int ColIndex = 0 RowIndex = 0;
int ColCount = myDGV.ColumnCount RowCount = myDGV.RowCount;
if (myDGV.RowCount == 0)
{
return 1;
}
// 创建Excel对象
Microsoft.Office.Interop.Excel.Application xlApp = new ApplicationClass();
if (xlApp == null)
{
return 2;
}
try
{
// 创建Excel工作薄
Workbook xlBook = xlApp.Workbooks.Add(true);
Worksheet xlSheet = (Worksheet)xlBook.Worksheets[1];
////Get excel Version
string Version = xlApp.Version;
//保存excel文件的格式
int FormatNum;
if (Convert.ToDouble(Version) < 12)
{
//使用Excel 97-2003
FormatNum = OLDOFFICEVESION;
}
else
{
//使用 excel 2007或更新
FormatNum = NEWOFFICEVESION;
}
// 设置标题
//标题所占的单元格数与DataGridView中的列数相同
Range range = xlSheet.get_Range(xlApp.Cells[1 1] xlApp.Cells[1 ColCount]);
range.MergeCells = true;
xlApp.ActiveCell.FormulaR1C1 = strCaption;
xlApp.ActiveCell.Font.Size = 20;
xlApp.ActiveCell.Font.B
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2019-03-20 17:32 UIForm4-备份\
目录 0 2019-03-20 17:32 UIForm4-备份\UIForm4\
文件 187 2019-03-20 08:00 UIForm4-备份\UIForm4\App.config
文件 5612 2019-03-20 11:37 UIForm4-备份\UIForm4\ExportDGVToExcel.cs
文件 6665 2019-03-20 11:21 UIForm4-备份\UIForm4\ExportToExcel.cs
文件 6599 2019-03-20 11:34 UIForm4-备份\UIForm4\Form1.Designer.cs
文件 11904 2019-03-20 15:56 UIForm4-备份\UIForm4\Form1.cs
文件 5817 2019-03-20 11:34 UIForm4-备份\UIForm4\Form1.resx
文件 1250 2019-03-20 10:16 UIForm4-备份\UIForm4\MySQLConn.cs
文件 519 2019-03-20 08:00 UIForm4-备份\UIForm4\Program.cs
目录 0 2019-03-20 17:32 UIForm4-备份\UIForm4\Properties\
文件 1352 2019-03-20 08:00 UIForm4-备份\UIForm4\Properties\AssemblyInfo.cs
文件 2866 2019-03-20 08:00 UIForm4-备份\UIForm4\Properties\Resources.Designer.cs
文件 5612 2019-03-20 08:00 UIForm4-备份\UIForm4\Properties\Resources.resx
文件 1094 2019-03-20 08:00 UIForm4-备份\UIForm4\Properties\Settings.Designer.cs
文件 249 2019-03-20 08:00 UIForm4-备份\UIForm4\Properties\Settings.settings
文件 5346 2019-03-20 11:32 UIForm4-备份\UIForm4\UIForm4.csproj
目录 0 2019-03-20 17:32 UIForm4-备份\UIForm4\bin\
目录 0 2019-03-20 17:32 UIForm4-备份\UIForm4\bin\Debug\
文件 1550200 2011-12-12 06:54 UIForm4-备份\UIForm4\bin\Debug\Microsoft.Office.Interop.Excel.dll
文件 3378414 2011-12-12 11:53 UIForm4-备份\UIForm4\bin\Debug\Microsoft.Office.Interop.Excel.xm
文件 63336 2011-12-12 06:54 UIForm4-备份\UIForm4\bin\Debug\Microsoft.Vbe.Interop.dll
文件 294912 2013-05-22 17:21 UIForm4-备份\UIForm4\bin\Debug\MySql.Data.dll
文件 17920 2019-03-20 11:37 UIForm4-备份\UIForm4\bin\Debug\UIForm4.exe
文件 187 2019-03-20 08:00 UIForm4-备份\UIForm4\bin\Debug\UIForm4.exe.config
文件 36352 2019-03-20 11:37 UIForm4-备份\UIForm4\bin\Debug\UIForm4.pdb
文件 22984 2019-03-20 11:40 UIForm4-备份\UIForm4\bin\Debug\UIForm4.vshost.exe
文件 187 2019-03-20 08:00 UIForm4-备份\UIForm4\bin\Debug\UIForm4.vshost.exe.config
文件 490 2015-06-04 12:48 UIForm4-备份\UIForm4\bin\Debug\UIForm4.vshost.exe.manifest
文件 448360 2011-12-12 06:54 UIForm4-备份\UIForm4\bin\Debug\office.dll
文件 1101614 2011-12-12 11:53 UIForm4-备份\UIForm4\bin\Debug\office.xm
............此处省略17个文件信息
- 上一篇:C#生成Excel表插件
- 下一篇:C#asp.net通过模板生成多页word
相关资源
- asp.net C#购物车源代码
- C#实时网络流量监听源码
- C#百度地图源码
- Visual C#.2010从入门到精通配套源程序
- C# 软件版本更新
- C#屏幕软键盘源码,可以自己定制界面
- 智慧城市 智能家居 C# 源代码
- c#获取mobile手机的IMEI和IMSI
- C#实现简单QQ聊天程序
- 操作系统 模拟的 欢迎下载 C#版
- C#写的计算机性能监控程序
- 用C#实现邮件发送,有点类似于outlo
- MVC model层代码生成器 C#
- c#小型图书销售系统
- C# Socket Server Client 通讯应用 完整的服
- c# winform 自动登录 百度账户 源代码
- C#编写的16进制计算器
- C#TCP通信协议
- C# 数据表(Dataset)操作 合并 查询一
- C#语音识别系统speechsdk51,SpeechSDK51L
- 数据库备份还原工具1.0 C# 源码
-
[免费]xm
lDocument 节点遍历C# - EQ2008LEDc#开发实例
- DirectX.Capturec# winform 操作摄像头录像附
- c# 实现的最大最小距离方法对鸢尾花
- C#版保龄球记分代码
- C#自定义控件
- 基于c#的实验室设备管理系统621530
- C# 使用ListView控件实现图片浏览器(源
- C#简单窗体聊天程序
评论
共有 条评论