C# Application Excel TreeView

三章 应用

20节
客户表登陆

//动软--单表--Models
--新建.net项目--简单三层管理--DBUtity--DbHelper.cs

21节
客户表数据读取

增加 CEnterprise(企事业单位;事业),CBirthday

22-24节
客户表添加、修改、删除

;select @@IDENTITY //返回刚插入的id

25节
补充MD5

using System.Security.Cryptography;

byte[] inBytes=Encoding.Default.GetBytes(str);
MD5 md5=new MD5CryptoServiceProvider();
byte[] outBytes=md5.ComputeHash(inBytes);
string lastStr=BitConverter.ToString(outBytes).Replace("-","");

26节
Excel简介

//18数字默认右边,可以选择存储格式
Workbook--sheet--row--cell
//123前面加‘123 变成以文本形式保存

net程序处理Excel的技术:
OLE Automation: 微软提供的,必须装Excel
Microsoft.Jet.OleDb: 微软提供的,不用装Excel
OpenXML: 只能处理xlsx docx pptx(为什么加个x,因为office2007及以后都支持xml打开文件,易于保存文件格式)
NPOI: 程序员自己开发的,开源; 不依赖Excel,节省了资源,没有安全性、性能的问题;
在ASP.NET中最适合,因为不用单独开一个进程就可以来处理Excel文件; 只能处理xls,不支持xlsx,(但现在新版本也可以支持xml打开的xlsx)

//百度--npoi--开源中国制作的npoi

27节
NPOI读数据

(姓名,年龄,联系方式,邮箱)
//Sheet右键改名字

//新建一个Console项目 NPOITest.csProj

//1 首先引入npoi操作的程序集
引用 NPOI.dll Ionic.Zip.dll
using NPOI.SS.UserModel; //包含对excel进行操作的方法
using NPOI.HSSF.UserModel; //包含excel每个sheet的属性

using (Stream stream = new FileStream("workbook1.xls", FileMode.Open))
{
IWorkbook workbook = new HSSFWorkbook(stream); //根据流实例化一个workbook
//Console.WriteLine(workbook.NumberOfSheets); //通过索引获得sheetName
for (int i = 0; i < workbook.NumberOfSheets;i++ )
{
ISheet sheet = workbook.GetSheetAt(i); //在指定index处获得sheet对象
Console.WriteLine(sheet.SheetName);
//获得row
//Console.WriteLine(sheet.LastRowNum);
for (int j = 0; j < sheet.LastRowNum;j++ )
{
IRow row = sheet.GetRow(j);
//获得cell
List<ICell> listICells = row.Cells; //获得行的所有cell对象
foreach(ICell cell in listICells)
{
Console.Write(cell.ToString() + "\t");
}
Console.WriteLine();
}
Console.WriteLine();
}
}

28节
NPOI写入数据

//创建一个workbook
IWorkbook workbook = new HSSFWorkbook();
ISheet sheet = workbook.CreateSheet("如鹏"); //创建sheet
IRow row = sheet.CreateRow(0); //创建row
ICell cell = row.CreateCell(0); //创建cell
cell.SetCellValue("rocky");
cell.SetCellType(CellType.STRING); //设置cell数据类型
//
using(Stream stream=File.Open("",FileMode.OpenOrCreate))
{
workbook.Write(stream);
}
Console.WriteLine("ok");

29节
项目导出

DAL:
CustomerInfoesToExcel()
//查询数据表
//创建workbook
//创建sheet
//每个reader.Read() 或dt.Rows 创建一个行
//创建首行
IRow headerRow=sheet.CreateRow(0);
for(int i=0;i<reader.FieldCount;i++)
{
//string fileName=reader.GetName(i);
ICell cellName=headerRow.CreateCell(i);
cellName.SetCellValue(reader.GetName(i));
}
int index=1;
while(reader.Read())
{
IRow row=sheet.CreateRow(index);
for(int i=0;i<reader.FieldCount;i++)
{
ICell cell=row.CreateCell(i);
cell.SetCellValue(reader.GetValue(i).ToString());
}
index++;
}
reader.Close();
using(Stream stream=File.Open("",FileMode.OpenOrCreate))
{
workbook.Write(stream);
}

30-31节
项目导入 -----------------------------------------------------------------------------------------------------------------------(*)

public void CustomerInfoesFormExcel()
{
string sql = @"insert into T_CustomerInfo(CName,CPwd,CNumber,CGender,CCompany,CDate,CMobile,CEmail,CAddress,DelFlag)
values(@CName,@CPwd,@CNumber,@CGender,@CCompany,@CDate,@CMobile,@CEmail,@CAddress,@DelFlag)";
//SqlParameter[] param = {
// new SqlParameter(){ParameterName="@CName",Value=model.CName},...
// };

//流读取
using(Stream stream=File.Open("workbookWrite.xls",FileMode.Open))
{
IWorkbook workbook = new HSSFWorkbook(stream);
ISheet sheet = workbook.GetSheetAt(0);

string[] parameterName = new string[sheet.GetRow(0).LastCellNum - 1]; //可以拿到最外面

//执行每一行
for (int i = 0; i <= sheet.LastRowNum;i++ )
{
IRow row = sheet.GetRow(i);
//初始化sql参数
SqlParameter[] param = new SqlParameter[row.LastCellNum - 1];
for (int j = 0; j < param.Length; j++)
{
param[j] = new SqlParameter();
}

if (i == 0) //首行
{
for (int j = 1; j < row.LastCellNum; j++)
{
ICell cell = row.GetCell(j);
parameterName[j-1] = "@" + cell.StringCellValue;
}
}
else //非首行的数据行
{
for (int j = 1; j < row.LastCellNum; j++)
{
ICell cell = row.GetCell(j);
param[j - 1].ParameterName = parameterName[j - 1];
param[j-1].Value = cell.ToString();
}
SqlHelper.ExecuteNonQuery(sql, param);
}
}
}
}

32节
汉字转换拼音

Visual Studio International Pack
Readme.html 说明书

CHnConversionPinYins.cs
引用 ChnCharInfo.dll
using Microdoft.International.Converters.PinYinConverter

ChineseChar chn=new ChineseChar("如"); //实例化一个汉字字符,构造函数传入一个汉字
//foreach(var item in chn.Pinyins) //通过属性拿到拼音
//{
// Console.WriteLine(item); //(RU2贰声) (RU5没有声调) (未知拼音)
//}
string pinyin=chin.Pinyins[0].Substring(0,chin.Pinyins[0].Length-1)
Console.WriteLine(pinyin);

foreach(cahe c in str){...} //遍历str中每个字符

33节
简体转繁体

引用 ChineseConverter
using Microsoft.International.Converters.TraditionalChineseToSimplifiedConverter;

string traditionStr = ChineseConverter.Convert("闯将明月光", ChineseConversionDirection.SimplifiedToTraditional);

34节
递归初阶

//方法体自己调用自己
StackOverflowException
//例1 累加

35节
Treeview

//树状数据结构

TreeNode node=new TreeNode("Adolph")
TreeNode n1=new TreeNode("好人");
TreeNode n2=new TreeNode("帅哥");
node.Nodes.Add(n1);
node.Nodes.Add(n2);
treeView1.Nodes.Add(node);

36-37节
TreeView省市绑定、递归绑定城区

GetAreaList(int AreaPId=0) //如果传参数,则为参数值;不传参,就是0

//加载节点
private void LoadTree()
{
List<AreaFull> list = areaBll.GetAreaByAreaPid();
foreach(AreaFull area in list)
{
TreeNode node = new TreeNode(area.AreaName);
node.Tag = area.AreaId;
BindCityInfo(node,area);
treeView1.Nodes.Add(node);
}
}

//绑定城市节点
private void BindCityInfo(TreeNode node,AreaFull area)
{
List<AreaFull> list = areaBll.GetAreaByAreaPid(area.AreaId);
foreach(AreaFull city in list)
{
TreeNode nodeCity = new TreeNode(city.AreaName);
nodeCity.Tag = city.AreaId;
BindCityInfo(nodeCity,city); //可以继续绑定加载城市下一级得区
node.Nodes.Add(nodeCity);
}
}

38节
递归删除

string areaName = treeView1.SelectedNode.Text
int id = (int)treeView1.SelectedNode.Tag

//根据id删除
private void DeleteNodesById(int id)
{
//如果是省,查询所有城市
List<AreaFull> list = areaBll.GetAreaByAreaPid(id);
//遍历城市,根据每个城市id查询所有城区
foreach(AreaFull area in list)
{
DeleteNodesById(area.AreaId);
//遍历城区,根据id删除城区
}
areaBll.DeleteAreaFullById(id); //从最里面往外删除
}

39节
编辑节点

treeView1_DoubleClick()
TreeNode node=treeView1.SelectedNode;
node.BeginEdit(); //设置为可编辑状态

BtnEditNode_Click()
nodeName
AreaId
//根据id更新地区节点

40节
添加节点

//选中一个节点,通过文本框,让用户输入一个节点,把这个节点添加到该节点的子节点中

//选中节点
//获得nodeName nodeId
//插入 insert into T(AreaName,AreaPid)

41节
三层架构总结

//CRUD 增删改查+MD5
//NPOI Excel导入导出
//Treeview 树节点的增删改查
BLL中ChangePassword

homework中最后一题:

DirectoryInfo dir=new DirectoryInfo("c:\soft\a"); //实例化一个指定目录
string dirName=dir.Name; //获得目录名称
dir.GetDirectories() //获得目录下的所有直接目录
dir.GetFiles() //获得目录下的所有直接文件

//最后一题作业:---> 遍历电脑指定文件夹下所有的文件和文件夹,加载到一个树控件中图片见附件

//加载树节点
private void LoadTree()
{
DirectoryInfo dir = new DirectoryInfo(@"G:\RuPeng_YZK_150107"); //实例化这个文件夹
string dirName = dir.Name;
TreeNode nodeRoot = new TreeNode(dirName);
BindDirectoryAndFile(nodeRoot, dir);
treeView1.Nodes.Add(nodeRoot);
}

//递归绑定加载子目录和子文件,nodeRoot是父节点
private void BindDirectoryAndFile(TreeNode nodeRoot, DirectoryInfo dir)
{
//获得子目录集合
DirectoryInfo[] directs = dir.GetDirectories();
foreach (DirectoryInfo direct in directs)
{
TreeNode node = new TreeNode(direct.Name);
nodeRoot.Nodes.Add(node);
//每一级子目录加入父节点后调用下一级
BindDirectoryAndFile(node, direct);
}
//获得子文件集合
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
TreeNode node = new TreeNode(file.Name);
nodeRoot.Nodes.Add(node);
}
}

时间: 2024-11-08 13:24:22

C# Application Excel TreeView的相关文章

.net 自己写的操作Excel 导入导出 类(以供大家参考和自己查阅)

由于现在网页很多都关系到Excel 的操作问题,其中数据的导入导出更是频繁,作为一个菜鸟,收集网上零散的知识,自己整合,写了一个Excel导入到GridView ,以及将GridView的数据导出到EXCEL的类方法,以供参考和方便自己以后查阅. 1 #region 引用部分 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Web; 6 using System.Dat

ExtAspNet从DataTable里导出Excel

protected void btn_ToExcel_Click(object sender, EventArgs e) { Response.ClearContent(); Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls"); Response.ContentType = "application/excel"; Response.W

C# 各种导入 Excel 文件的数据的方法总结

在导入之前都需要将上传的文件保存到服务器,所以避免重复的写这些代码,先贴出上传文件并保存到服务器指定路径的代码. protected void btnImport_Click(object sender, EventArgs e) { Random random = new Random(); ImportClass Import = new ImportClass(); //保存文件的虚拟路径 string path = "Import/"; //获取选择的文件名 string fi

Share data between VSTO and Excel DNA App domains

Is there a way to share data between a VSTO add in and an Excel DNA add in? Or can the Excel DNA add in be loaded into the VSTO's app domain? The Excel-DNA add-in will always be in a separate AppDomain. You might try to pass an object via the AddIn's

Excel和SQLserver数据互相存取

#region filed DataSet dataSet; #endregion #region Excel导出到DataBase /// <summary> ///从Excel中导入到到DataSet /// </summary> /// <param name="filePath">Excel文件路径</param> public void ImportToDataBase(string filePath) { GetExcelDa

[转自他人博客]Excel worksheet拷贝

void Cp() { OpenFileDialog openFileDialog1 = new OpenFileDialog();            openFileDialog1.Title = "请选择源文件 ";            openFileDialog1.Filter = "Excel文件(*.xls)|*.xls ";            openFileDialog1.ShowDialog(); if (openFileDialog1.

C#将dataGridView中显示的数据导出到Excel(超实用版)

using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Office.Interop; using Microsoft.Office.Interop.Excel; using System.Windows.Forms; using Excel = Microsoft.Office.Interop.Excel; public class ExprotT

c# excel sheep 导出

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; using System.Reflection; using System.Collections; using Microsoft.Office.Interop.Excel; namespace Common { public class ExcelExportHelper { /// <

C# 读取EXCEL文件的三种经典方法

1.方法一:采用OleDB读取EXCEL文件: 把EXCEL文件当做一个数据源来进行数据的读取操作,实例如下: public DataSet ExcelToDS(string Path) { string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +"Data Source="+ Path +";"+"Extended Properties=Excel 8.0;"; OleDb