基本信息
源码名称:C# excel文件 读取和写入操作示例
源码大小:10.04M
文件格式:.rar
开发语言:C#
更新时间:2019-08-01
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 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 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();
}
}
}