在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#文件解析和处理 → C# excel文件 读取和写入操作示例

C# excel文件 读取和写入操作示例

C#文件解析和处理

下载此实例
  • 开发语言:C#
  • 实例大小:10.04M
  • 下载次数:161
  • 浏览次数:800
  • 发布时间:2019-08-01
  • 实例类别:C#文件解析和处理
  • 发 布 人:liuyujun2006
  • 文件格式:.rar
  • 所需积分:2
 相关标签: Excel C#

实例介绍

【实例简介】

【实例截图】

from clipboard

【核心代码】

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 CSharpJExcel.Jxl;
using CSharpJExcel.Jxl.Write;
using System.Collections;
using System.IO;

namespace ExcelReadWriter
{
    public partial class Form1 : Form
    {
        private List<string> dataList = new List<string>();
        private List<string> dataListcpr = new List<string>();
        private RelationExchange RE=new RelationExchange();
        private Dictionary<string, string> exchange = new Dictionary<string, string>();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog pf = new OpenFileDialog();
            if (pf.ShowDialog() == DialogResult.OK)
            {
                dataList = RE.GetColumnData(pf.FileName, "Sheet1", 8);                                                       
            }
        }
        public bool compare4(List<string> list1, string str)
        {
            if(list1.Contains(str))
                return true;
            string[] str1= str.Split('.');
            string str2;
            string[] str3;
            if (str1.Length> 3)
            {
                for (int k = 0; k < list1.Count(); k  )
                {
                    str3 = list1[k].Split('.');
                    if (str3.Length >= 3)
                    {
                        if (str1[0] == str3[0] && str1[1] == str3[1] && str1[2] == str3[2])
                            return true;
                    }
                }
            }
            return false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var ws = new WorkbookSettings();
            WritableWorkbook wr = Workbook.createWorkbook(new System.IO.FileInfo("D:\\输出.xls"), ws);
            WritableSheet sheet = wr.createSheet("案例", 0);

            List<string> tepList = new List<string>();

            for (int i = 0; i < dataList.Count(); i  )
            {
                string temstr = dataList[i].Trim();
                string[] strs = temstr.Split('\n');
                tepList.Clear();
                for (int j = 0; j < strs.Length; j  )
                {
                    string tmpoutstring = "";
                    string[] strs1 = strs[j].Split(' ');
                    if (strs1[0].Length >= 11)
                    {
                        tmpoutstring = tmpoutstring   "FUN-SS026-";

                        string str5 = strs1[0].Substring(11);
                        for (int k = 0; k < str5.Length; k  )
                        {
                            if (str5[k] == '.'||(str5[k] >= '0' && str5[k] <= '9'))
                                tmpoutstring = tmpoutstring   str5[k];
                        }                        
                    }
                    /*完全对应
                    if (!tepList.Contains(tmpoutstring)&&dataListcpr.Contains(tmpoutstring))
                        tepList.Add(tmpoutstring);
                     *  && compare4(dataListcpr,tmpoutstring)
                     * */
                    if (!tepList.Contains(tmpoutstring))
                        tepList.Add(tmpoutstring);
                }
                string output="";

                for (int m = 0; m < tepList.Count(); m  )
                {
                    if (m != tepList.Count() - 1)
                    {
                        output = output   tepList[m]   "\n";
                    }
                    else
                        output = output   tepList[m];
                }
                
                WritableCell cell = new CSharpJExcel.Jxl.Write.Label(0, i,output);
                sheet.addCell(cell);
            }
            wr.write();
            wr.close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog pf = new OpenFileDialog();
            if (pf.ShowDialog() == DialogResult.OK)
            {
                dataListcpr=RE.GetColumnData(pf.FileName, "Topo export-对应关系整理", 1);                         
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            List<DataCollection> listCollection = new List<DataCollection>();            
            //Hashtable hscollection = new Hashtable();
            string xuhao1 = "", xuqiu1 = "";
            string[] xuqiu2;                 
            OpenFileDialog pf = new OpenFileDialog();
            if (pf.ShowDialog() == DialogResult.OK)
            {
                string fileName = pf.FileName;
                RE.ExchangeData(pf.FileName, "1", 0, 1);
            }
        }



        private void button5_Click_1(object sender, EventArgs e)
        {
            List<RelationTable> relationTable = new List<RelationTable>();
            List<RelationTable> relationTable1 = new List<RelationTable>();
            RelationTable RT=new RelationTable();
            OpenFileDialog pf = new OpenFileDialog();
            if (pf.ShowDialog() == DialogResult.OK)
            {
                string fileName = pf.FileName;
                int num = 1;
                string[] modes = new string[] { "FS","CO","OS","SH","SL","SB","TR","PT","PS","CS","IS"};
                for (int i = 0; i < modes.Length; i  )
                {
                    relationTable = RT.GetColumnData(fileName, modes[i], ref num);
                    foreach (RelationTable rt in relationTable)
                        relationTable1.Add(rt);
                }
            }



            var ws = new WorkbookSettings();
            WritableWorkbook wr = Workbook.createWorkbook(new System.IO.FileInfo("D:\\对应表.xls"), ws);
            WritableSheet sheet = wr.createSheet("对应关系", 0);
            WritableCell cell, cell1, cell2, cell3, cell4;


            cell = new CSharpJExcel.Jxl.Write.Label(0,0, "序号");
            sheet.addCell(cell);
            cell1 = new CSharpJExcel.Jxl.Write.Label(1, 0, "铁科案例编号");
            sheet.addCell(cell1);
            cell2 = new CSharpJExcel.Jxl.Write.Label(2,0, "铁总案例编号");
            sheet.addCell(cell2);
            cell3 = new CSharpJExcel.Jxl.Write.Label(3, 0, "模式");
            sheet.addCell(cell3);
            cell4 = new CSharpJExcel.Jxl.Write.Label(4,0, "需求编号");
            sheet.addCell(cell4);
            
            for (int jj = 0; jj < relationTable1.Count(); jj  )
            {
                 cell = new CSharpJExcel.Jxl.Write.Label(0, jj 1, relationTable1[jj].S_xuHao);
                sheet.addCell(cell);
                 cell1 = new CSharpJExcel.Jxl.Write.Label(1, jj 1, relationTable1[jj].S_caseTieKe);
                sheet.addCell(cell1);
                 cell2 = new CSharpJExcel.Jxl.Write.Label(2, jj 1, relationTable1[jj].S_caseTieZong);
                sheet.addCell(cell2);
                 cell3 = new CSharpJExcel.Jxl.Write.Label(3, jj 1, relationTable1[jj].S_mode);
                sheet.addCell(cell3);
                 cell4 = new CSharpJExcel.Jxl.Write.Label(4, jj 1, relationTable1[jj].S_srs);
                sheet.addCell(cell4);
            }
            wr.write();
            wr.close();

        }

        private void button6_Click(object sender, EventArgs e)
        {
            List<RelationTable> relationTable = new List<RelationTable>();
            List<RelationTable> relationTable1 = new List<RelationTable>();
            RelationTable RT = new RelationTable();
            OpenFileDialog pf = new OpenFileDialog();
            if (pf.ShowDialog() == DialogResult.OK)
            {
                string fileName = pf.FileName;
                int num = 1;
                string[] modes = new string[] { "FS", "CO", "OS", "SH", "SL", "SB", "PS", "CS", "RO", "IS" };
                for (int i = 0; i < modes.Length; i  )
                {
                    relationTable = RT.GetColumnData(fileName, modes[i], ref num);
                    foreach (RelationTable rt in relationTable)
                        relationTable1.Add(rt);
                }
            }



            var ws = new WorkbookSettings();
            WritableWorkbook wr = Workbook.createWorkbook(new System.IO.FileInfo("D:\\对应表.xls"), ws);
            WritableSheet sheet = wr.createSheet("对应关系", 0);
            WritableCell cell, cell1, cell2, cell3, cell4;


            cell = new CSharpJExcel.Jxl.Write.Label(0, 0, "序号");
            sheet.addCell(cell);
            cell1 = new CSharpJExcel.Jxl.Write.Label(1, 0, "铁科案例编号");
            sheet.addCell(cell1);
            cell2 = new CSharpJExcel.Jxl.Write.Label(2, 0, "HLS案例编号");
            sheet.addCell(cell2);
            cell3 = new CSharpJExcel.Jxl.Write.Label(3, 0, "模式");
            sheet.addCell(cell3);
            cell4 = new CSharpJExcel.Jxl.Write.Label(4, 0, "需求编号");
            sheet.addCell(cell4);

            for (int jj = 0; jj < relationTable1.Count(); jj  )
            {
                cell = new CSharpJExcel.Jxl.Write.Label(0, jj   1, relationTable1[jj].S_xuHao);
                sheet.addCell(cell);
                cell1 = new CSharpJExcel.Jxl.Write.Label(1, jj   1, relationTable1[jj].S_caseTieKe);
                sheet.addCell(cell1);
                cell2 = new CSharpJExcel.Jxl.Write.Label(2, jj   1, relationTable1[jj].S_caseTieZong);
                sheet.addCell(cell2);
                cell3 = new CSharpJExcel.Jxl.Write.Label(3, jj   1, relationTable1[jj].S_mode);
                sheet.addCell(cell3);
                cell4 = new CSharpJExcel.Jxl.Write.Label(4, jj   1, relationTable1[jj].S_srs);
                sheet.addCell(cell4);
            }
            wr.write();
            wr.close();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            OpenFileDialog pf = new OpenFileDialog();
            if (pf.ShowDialog() == DialogResult.OK)
            {
                exchange = RE.GetColumnData(pf.FileName, "1", 0,1);
                MessageBox.Show("OK");
            }
            
        }

        private void button8_Click(object sender, EventArgs e)
        {
            string str = "With Selection.Find"   "\r\n";//" Selection.Find.ClearFormatting" "\r\n"  "Selection.Find.Replacement.ClearFormatting"   "\r\n"   " .With Selection.Find"   "\r\n";
            string str1 = ".Forward = True"   "\r\n"   " .Wrap = wdFindContinue"   "\r\n"   ".Format = False"   "\r\n"   ".MatchCase = False"   "\r\n"   ".MatchWholeWord = False"   "\r\n"   ".MatchByte = True"   "\r\n"   ".MatchWildcards = False"   "\r\n"   ".MatchSoundsLike = False"   "\r\n"   ".MatchAllWordForms = False"   "\r\n"   "End With"   "\r\n"  "Selection.Find.Execute Replace:= wdReplaceAll"   "\r\n";//  "Selection.Find.ClearFormatting"   "\r\n"   "Selection.Find.Replacement.ClearFormatting"   "\r\n"   "Selection.Find.Execute Replace:= wdReplaceAll"   "\r\n"   "Selection.Find.Execute Replace:= wdReplaceAll"   "\r\n";
            //  .Text = "CASE-C3-BALISE25"
            // .Replacement.Text = "3.4"
            string path = @"D:\1.txt";
            //if(File.Exists(path))o
            //{

            //}
            

            foreach(string st in exchange.Keys)
            {       
                //string str3=string.Format(".Text = " """ {0} )  
                string str2=str  ".Text = " "\"" st "\"" "\r\n";
                str2 =".Replacement.Text ="   "\""   exchange[st]   "\""   "\r\n";
                str2  = str1;
                File.AppendAllText(path, str2);
            }
          //  FileStream fs = new FileStream(@"D:\1.txt", FileMode.Append);
          
        }

        private void button9_Click(object sender, EventArgs e)
        {
            string str = "Columns(" "\"" "A:E" "\"" ").Select"   "\r\n";
          //  string str1 = ".Forward = True"   "\r\n"   " .Wrap = wdFindContinue"   "\r\n"   ".Format = False"   "\r\n"   ".MatchCase = False"   "\r\n"   ".MatchWholeWord = False"   "\r\n"   ".MatchByte = True"   "\r\n"   ".MatchWildcards = False"   "\r\n"   ".MatchSoundsLike = False"   "\r\n"   ".MatchAllWordForms = False"   "\r\n"   "End With"   "\r\n"   "Selection.Find.Execute Replace:= wdReplaceAll"   "\r\n";//  "Selection.Find.ClearFormatting"   "\r\n"   "Selection.Find.Replacement.ClearFormatting"   "\r\n"   "Selection.Find.Execute Replace:= wdReplaceAll"   "\r\n"   "Selection.Find.Execute Replace:= wdReplaceAll"   "\r\n";
            string path = @"D:\1.txt";
       


            foreach (string st in exchange.Keys)
            {
                //string str2 = str   ".Text = "   "\""   st   "\""   "\r\n";
                //str2  = ".Replacement.Text ="   "\""   exchange[st]   "\""   "\r\n";
                //str2  = str1;
                string str2 = str   "Selection.Replace What:="   "\""   st   "\""   ", Replacement:="   "\""   exchange[st]   "\""   ", LookAt:=xlPart, _"   "\r\n"   "SearchOrder:= xlByRows, MatchCase:= False, SearchFormat:= False, _"   "\r\n"   "ReplaceFormat:= False"   "\r\n";
                File.AppendAllText(path, str2);
            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            OpenFileDialog pf = new OpenFileDialog();
            if (pf.ShowDialog() == DialogResult.OK)
            {
                dataList = RE.GetColumnData(pf.FileName, "1", 0);
            }

            var ws = new WorkbookSettings();
            WritableWorkbook wr = Workbook.createWorkbook(new System.IO.FileInfo("D:\\1.xls"), ws);
            WritableSheet sheet = wr.createSheet("1", 0);

            for (int i = 0; i < dataList.Count(); i  )
            {              
                  string output = "";
                string[] str = dataList[i].Split(';');
                for(int j=0;j<str.Length;j  )
                {
                    if(!str[j].Contains("FUN")&&str[j].Trim()!="")
                    {
                        output =output  str[j].Trim() ";";
                    }
                }                

                WritableCell cell = new CSharpJExcel.Jxl.Write.Label(0, i, output);
                sheet.addCell(cell);
            }
            wr.write();
            wr.close();
        }
    }
}

标签: Excel C#

实例下载地址

C# excel文件 读取和写入操作示例

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警