基本信息
源码名称:C# 打开操作Excel 例子
源码大小:0.30M
文件格式:.rar
开发语言:C/C++
更新时间:2015-11-17
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
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 Microsoft.Office.Interop.Excel;
using System.Reflection;
using Excel = Microsoft.Office.Interop.Excel;
using System.IO;
namespace Excel_data
{
public partial class Form1 : Form
{
public CCExcel excel;
public Form1()
{
InitializeComponent();
}
private void Form1Closing_Click(object sender, EventArgs e)
{
GC.Collect();
}
private void butRun_Click(object sender, EventArgs e)
{
this.Save2Excel();
}
private void Save2Excel()
{
//string file = "F:\\11\\ck.xlsx";
//string path = "F:\\11\\excel\\";
string time = DateTime.Now.ToString("yyyyMMdd");
string file = System.Windows.Forms.Application.StartupPath "\\ck.xlsx";
string path = System.Windows.Forms.Application.StartupPath "\\excel\\";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string saveName1 = path "_" DateTime.Now.ToString("yyyyMMddHHmmss") ".xlsx";
excel = new CCExcel(file,saveName1);
excel.Exceldata();
}
}
public class CCExcel
{
public Excel.Application appExcel;
public Excel.Workbooks wbs;
public Excel.Workbook wb;
public Excel.Worksheets wss;
public Excel.Worksheet ws;
private string fileName;
private string saveName;
public CCExcel(string fileName,string saveName)
{
//
//TODO: 在此处添加构造函数逻辑
//
this.fileName = fileName;
this.saveName = saveName;
}
public void Exceldata()
{
Create();
//Open(fileName);
Data2Excel();
Save(saveName);
//Save(wb,saveName);
//appExcel.ActiveWorkbook.SaveCopyAs(fileName); //判断当前激活的表,并保存这个表。否则,保存时会弹出“是否保存Sheet1.xlsx”的对话框
wb.Close(Type.Missing, Type.Missing, Type.Missing);
wbs.Close();
appExcel.Quit();
wb = null;
wbs = null;
appExcel = null;
GC.Collect();
}
private void Create()//创建一个Excel对象
{
appExcel = new Excel.Application();
wbs = appExcel.Workbooks;
wb = wbs.Add(true);
//ws = (Excel.Worksheet)wb.ActiveSheet;//这是一个只读sheets集合
//Excel.Worksheet worksheet = wb.ActiveSheet as Excel.Worksheet;//这也是一个只读sheets集合
//Excel.Worksheet ws = (Worksheet)wb.Worksheets[1];//创建工作页sheet单页
ws = wb.Worksheets[1] as Worksheet; //第一个sheet页
ws.Name = "ck"; //这里修改sheet名称
}
public void Open(string fileName)
{
appExcel = new Excel.Application();
wbs = appExcel.Workbooks;
wb = wbs.Add(fileName);
wb = wbs.Open(fileName,Type.Missing,Type.Missing,Type.Missing,Type.Missing,
Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,
Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
}
private void Data2Excel()
{
ws.Cells[4, 1] = 11;
ws.Cells[4, 2] = 22;
ws.Cells[4, 3] = 33;
ws.Cells[4, 4] = 44;
ws.Cells[4, 5] = 55;
}
private void Save(string saveName)//文档另存
//private bool Save(Microsoft.Office.Interop.Excel.Workbook wb,object saveName)
{
try
{
wb.SaveAs(saveName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Excel.XlSaveAsAccessMode.xlExclusive,Type.Missing,Type.Missing,Type.Missing,
Type.Missing,Type.Missing);
}
catch (Exception ex)
{
//return false;
throw ex;
}
//return true;
}
}
}