C#生成Excell代码
来源:互联网 发布:预算软件有哪几种 编辑:程序博客网 时间:2024/06/02 01:53
应导入的命名空间:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;using Microsoft.Office.Interop.Excel;using ExcelApplication = Microsoft.Office.Interop.Excel.ApplicationClass;using Microsoft.Win32;using System.IO;using System.Reflection;
生成部分:
private void button2_Click(object sender, EventArgs e) { //申明保存对话框 SaveFileDialog dlg = new SaveFileDialog(); //默然文件后缀 dlg.DefaultExt = "xls "; //文件后缀列表 dlg.Filter = "EXCEL文件(*.XLS)|*.xls "; //默然路径是系统当前路径 dlg.InitialDirectory = Directory.GetCurrentDirectory(); //打开保存对话框 if (dlg.ShowDialog() == DialogResult.Cancel) return; //返回文件路径 string fileNameString = dlg.FileName; //验证strFileName是否为空或值无效 if (fileNameString.Trim() == " ") return; //定义表格内数据的行数和列数 int rowscount = dataGridView1.Rows.Count; int colscount = dataGridView1.Columns.Count; //行数必须大于0 if ( rowscount <= 0 ) { MessageBox.Show("没有数据可供保存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } //列数必须大于0 if (colscount <= 0) { MessageBox.Show("没有数据可供保存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } //行数不可以大于65536 if (rowscount > 65536) { MessageBox.Show("数据记录数太多(最多不能超过65536条),不能保存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } //列数不可以大于255 if (colscount > 255) { MessageBox.Show("数据记录行数太多,不能保存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } //验证以fileNameString命名的文件是否存在,如果存在删除它 FileInfo file = new FileInfo(fileNameString); if (file.Exists) { try { file.Delete(); } catch (Exception error) { MessageBox.Show(error.Message, "删除失败 ", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } } //创建空EXCEL对象 Microsoft.Office.Interop.Excel.Application objExcel = null; Microsoft.Office.Interop.Excel.Workbook objWorkbook = null; Microsoft.Office.Interop.Excel.Worksheet objSheet = null; try { //申明对象 objExcel = new Microsoft.Office.Interop.Excel.Application(); objWorkbook = objExcel.Workbooks.Add(Missing.Value); objSheet = (Microsoft.Office.Interop.Excel.Worksheet)objWorkbook.ActiveSheet;//强制类型转换 //设置EXCEL不可见(后台运行) objExcel.Visible = false; //向Excel中写入表格的表头 int displayColumnsCount = 1; for (int i = 0; i <= dataGridView1.ColumnCount - 1; i++) { if (dataGridView1.Columns[i].Visible == true) { objExcel.Cells[1, displayColumnsCount] = dataGridView1.Columns[i].HeaderText.Trim(); displayColumnsCount++; } } //向Excel中逐行逐列写入表格中的数据 for (int row = 0; row <= dataGridView1.RowCount - 1; row++) { displayColumnsCount = 1; for (int col = 0; col < colscount; col++) { if (dataGridView1.Columns[col].Visible == true) { try { objExcel.Cells[row + 2, displayColumnsCount] = dataGridView1.Rows[row].Cells[col].Value.ToString().Trim(); displayColumnsCount++; } catch (Exception) { } } } } //保存文件 objWorkbook.SaveAs(fileNameString, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); } catch (Exception error) { MessageBox.Show(error.Message, "警告 ", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } finally { //关闭Excel应用 if (objWorkbook != null) objWorkbook.Close(Missing.Value, Missing.Value, Missing.Value); if (objExcel.Workbooks != null) objExcel.Workbooks.Close(); if (objExcel != null) objExcel.Quit(); objSheet = null; objWorkbook = null; objExcel = null; } MessageBox.Show(fileNameString + "\n\n导出完毕! ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
- C#生成Excell代码
- 转--------C#访问Excell
- C#中写Excell
- c#操作excell
- C# 导出Excell
- 利用poi生成excell文件
- C#操作Excell常用方法
- C#操作Excell常用方法
- C#操作Excell常用方法
- EXCELL
- 生成csv文件 可以使用excell读取
- ASP.NET C# Excell导入导出
- C#生成缩略图代码
- C#生成Word代码
- C#生成缩略图代码
- c# 曲线图生成代码
- C#生成缩略图代码
- C#二维码生成代码
- 数据的游戏:冰与火
- 大数据及格式化数据的简单实现
- C#导入Excel2010出现加载DLL失败或者类库未注册的问题,0x8002801D或者0x80029C4A
- 移位操作---左移和右移
- 一站搞定Python开发:Python精品学习资料集
- C#生成Excell代码
- 如何在通信协议中传输浮点数
- org.apache.commons.lang.DateUtils的用法
- 关于Xcode5,4,3版本间的Add Headers
- 【Linux】Linux 标准目录结构
- Js动态日期时间显示(简单易学实用版)
- dwr和query的优缺点
- ibatis使用文档(下篇)
- Android中xml文件中设置背景颜色、图片