基本信息
源码名称:EXCEL实例
源码大小:0.38M
文件格式:.zip
开发语言:C#
更新时间:2022-03-25
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 1 元×
微信扫码支付:1 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
EXCEL实例
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Excel;
using System.Text.RegularExpressions;
namespace WindowsFormsApp1
{
class myExcel
{
Microsoft.Office.Interop.Excel.Application mExcle = null; //new Microsoft.Office.Interop.Excel.Application();
public void CreatExcel(string path= @"E:\章丘实验室\孙嘉泽\水泥、矿粉\矿粉\矿粉2022试验报告.xlsx") {
mExcle =new Microsoft.Office.Interop.Excel.Application();
mExcle.Workbooks.Open(path);
mExcle.Visible = true;
}
public void setActiveSheet(string sheetname) {
Sheets sheets = mExcle.Sheets;
Worksheet worksheet = null;
for (int i =1; i < sheets.Count;i )
{
worksheet = sheets.Item[i];
if (worksheet.Name.Equals(sheetname)) {
worksheet.Activate();
}
}
}
public void setActiveSheet(int sheetnum)
{
Worksheet worksheet = mExcle.Sheets.Item[sheetnum];
worksheet.Activate();
}
public void deleteSheet() {
mExcle.ActiveWorkbook.
}
public string readCell(string cell)
{
int colnum = ToColum(cell);
int row = ToROW(cell);
string text = mExcle.Cells[colnum][row].text;
Console.WriteLine("colnum=" colnum "row=" row);
return text;
}
public string WriteCell(string text,string cell )
{
int colnum = ToColum(cell);
int row = ToROW(cell);
Range range= (Range)mExcle.Cells[colnum][row];
range.Value = text;
return text;
}
public string WriteCell(string text, int colnum,int row)
{
Range range = (Range)mExcle.Cells[colnum][row];
range.Value = text;
return text;
}
public static int ToColum(string columnName)
{
columnName = Regex.Replace(columnName, "[0-9]", "", RegexOptions.IgnoreCase);
if (!Regex.IsMatch(columnName.ToUpper(), @"[A-Z] ")) { throw new Exception("invalid parameter"); }
int index = 0;
char[] chars = columnName.ToUpper().ToCharArray();
for (int i = 0; i < chars.Length; i )
{
index = ((int)chars[i] - (int)'A' 1) * (int)Math.Pow(26, chars.Length - i - 1);
}
return index ;
}
public static int ToROW(string columnName)
{
string strRemoved = Regex.Replace(columnName, "[A-Z]", "", RegexOptions.IgnoreCase);
int k = int.Parse(strRemoved);
return k;
}
public static string ToName(int index)
{
if (index < 0) { throw new Exception("invalid parameter"); }
List<string> chars = new List<string>();
do
{
if (chars.Count > 0) index--;
chars.Insert(0, ((char)(index % 26 (int)'A')).ToString());
index = (int)((index - index % 26) / 26);
} while (index > 0);
return String.Join(string.Empty, chars.ToArray());
}
}
}
EXCEL实例
Excel操作
using System;
using System.Collections.Generic;using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Excel;
using System.Text.RegularExpressions;
namespace WindowsFormsApp1
{
class myExcel
{
Microsoft.Office.Interop.Excel.Application mExcle = null; //new Microsoft.Office.Interop.Excel.Application();
public void CreatExcel(string path= @"E:\章丘实验室\孙嘉泽\水泥、矿粉\矿粉\矿粉2022试验报告.xlsx") {
mExcle =new Microsoft.Office.Interop.Excel.Application();
mExcle.Workbooks.Open(path);
mExcle.Visible = true;
}
public void setActiveSheet(string sheetname) {
Sheets sheets = mExcle.Sheets;
Worksheet worksheet = null;
for (int i =1; i < sheets.Count;i )
{
worksheet = sheets.Item[i];
if (worksheet.Name.Equals(sheetname)) {
worksheet.Activate();
}
}
}
public void setActiveSheet(int sheetnum)
{
Worksheet worksheet = mExcle.Sheets.Item[sheetnum];
worksheet.Activate();
}
public void deleteSheet() {
mExcle.ActiveWorkbook.
}
public string readCell(string cell)
{
int colnum = ToColum(cell);
int row = ToROW(cell);
string text = mExcle.Cells[colnum][row].text;
Console.WriteLine("colnum=" colnum "row=" row);
return text;
}
public string WriteCell(string text,string cell )
{
int colnum = ToColum(cell);
int row = ToROW(cell);
Range range= (Range)mExcle.Cells[colnum][row];
range.Value = text;
return text;
}
public string WriteCell(string text, int colnum,int row)
{
Range range = (Range)mExcle.Cells[colnum][row];
range.Value = text;
return text;
}
public static int ToColum(string columnName)
{
columnName = Regex.Replace(columnName, "[0-9]", "", RegexOptions.IgnoreCase);
if (!Regex.IsMatch(columnName.ToUpper(), @"[A-Z] ")) { throw new Exception("invalid parameter"); }
int index = 0;
char[] chars = columnName.ToUpper().ToCharArray();
for (int i = 0; i < chars.Length; i )
{
index = ((int)chars[i] - (int)'A' 1) * (int)Math.Pow(26, chars.Length - i - 1);
}
return index ;
}
public static int ToROW(string columnName)
{
string strRemoved = Regex.Replace(columnName, "[A-Z]", "", RegexOptions.IgnoreCase);
int k = int.Parse(strRemoved);
return k;
}
public static string ToName(int index)
{
if (index < 0) { throw new Exception("invalid parameter"); }
List<string> chars = new List<string>();
do
{
if (chars.Count > 0) index--;
chars.Insert(0, ((char)(index % 26 (int)'A')).ToString());
index = (int)((index - index % 26) / 26);
} while (index > 0);
return String.Join(string.Empty, chars.ToArray());
}
}
}
.
├── WindowsFormsApp1
│ ├── WindowsFormsApp1
│ │ ├── Form1.Designer.cs
│ │ ├── Form1.cs
│ │ ├── Form1.resx
│ │ ├── Program.cs
│ │ ├── Properties
│ │ │ ├── AssemblyInfo.cs
│ │ │ ├── Resources.Designer.cs
│ │ │ ├── Resources.resx
│ │ │ ├── Settings.Designer.cs
│ │ │ └── Settings.settings
│ │ ├── WindowsFormsApp1.csproj
│ │ ├── bin
│ │ │ └── Debug
│ │ │ ├── WindowsFormsApp1.exe
│ │ │ ├── WindowsFormsApp1.pdb
│ │ │ └── lpk.dll
│ │ ├── myExcel.cs
│ │ ├── obj
│ │ │ └── Debug
│ │ │ ├── DesignTimeResolveAssemblyReferences.cache
│ │ │ ├── DesignTimeResolveAssemblyReferencesInput.cache
│ │ │ ├── TempPE
│ │ │ ├── WindowsFormsApp1.Form1.resources
│ │ │ ├── WindowsFormsApp1.Properties.Resources.resources
│ │ │ ├── WindowsFormsApp1.csproj.CoreCompileInputs.cache
│ │ │ ├── WindowsFormsApp1.csproj.FileListAbsolute.txt
│ │ │ ├── WindowsFormsApp1.csproj.GenerateResource.cache
│ │ │ ├── WindowsFormsApp1.csproj.ResolveComReference.cache
│ │ │ ├── WindowsFormsApp1.exe
│ │ │ ├── WindowsFormsApp1.pdb
│ │ │ └── lpk.dll
│ │ ├── 安置房项目业务明细表.cs
│ │ └── 预拌混凝土出厂检验台帐.cs
│ └── WindowsFormsApp1.sln
└── 好例子网_WindowsFormsApp1.zip
8 directories, 29 files
├── WindowsFormsApp1
│ ├── WindowsFormsApp1
│ │ ├── Form1.Designer.cs
│ │ ├── Form1.cs
│ │ ├── Form1.resx
│ │ ├── Program.cs
│ │ ├── Properties
│ │ │ ├── AssemblyInfo.cs
│ │ │ ├── Resources.Designer.cs
│ │ │ ├── Resources.resx
│ │ │ ├── Settings.Designer.cs
│ │ │ └── Settings.settings
│ │ ├── WindowsFormsApp1.csproj
│ │ ├── bin
│ │ │ └── Debug
│ │ │ ├── WindowsFormsApp1.exe
│ │ │ ├── WindowsFormsApp1.pdb
│ │ │ └── lpk.dll
│ │ ├── myExcel.cs
│ │ ├── obj
│ │ │ └── Debug
│ │ │ ├── DesignTimeResolveAssemblyReferences.cache
│ │ │ ├── DesignTimeResolveAssemblyReferencesInput.cache
│ │ │ ├── TempPE
│ │ │ ├── WindowsFormsApp1.Form1.resources
│ │ │ ├── WindowsFormsApp1.Properties.Resources.resources
│ │ │ ├── WindowsFormsApp1.csproj.CoreCompileInputs.cache
│ │ │ ├── WindowsFormsApp1.csproj.FileListAbsolute.txt
│ │ │ ├── WindowsFormsApp1.csproj.GenerateResource.cache
│ │ │ ├── WindowsFormsApp1.csproj.ResolveComReference.cache
│ │ │ ├── WindowsFormsApp1.exe
│ │ │ ├── WindowsFormsApp1.pdb
│ │ │ └── lpk.dll
│ │ ├── 安置房项目业务明细表.cs
│ │ └── 预拌混凝土出厂检验台帐.cs
│ └── WindowsFormsApp1.sln
└── 好例子网_WindowsFormsApp1.zip
8 directories, 29 files