zoukankan      html  css  js  c++  java
  • 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() + " ");
    }
    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:softa"); //实例化一个指定目录
    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);
    }
    }

  • 相关阅读:
    冰淇淋主题博客皮肤
    在input内放置小图标的方法
    制作表单(第一期笔记):美化表单常用的css样式
    CSS:linear-gradient()背景颜色渐变--练习笔记
    css中的vertical-align在内联元素中的表现--垂直对齐(带图示)
    利用border特性做的几个纯css小图标
    CSS文本、字体个别属性效果对比
    CSS文本、字体属性(更新中)
    JavaScript HTML DOM 学习笔记
    JS、JQ获取容器内标签的个数
  • 原文地址:https://www.cnblogs.com/adolphyang/p/4757882.html
Copyright © 2011-2022 走看看