快捷搜索:

VS2017下编写C#程序读写Excel文件

有时候我们需要对Excel文件进行特殊处理,这种情况下写个小程序是很方便的。最近刚开始学C#,正好有同学需要处理个Excel文件,我就写了一个程序。发现网上虽有资料,但很多行不通,特此记录,以免后来人少走弯路。

正文:

首先,界面如下,当然,程序员不需要看界面,从代码中就可以直接学习。

然后,需要下载一个文件“Microsoft.Office.Interop.Excel.dll”,自行网搜。导入到引用之中。然后也把System.Windows.Forms加入引用。

最后,上代码。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;

//ghw:add

using System.Windows.Forms;

using Excel = Microsoft.Office.Interop.Excel;

namespace WpfExcel

{

///<summary>

/// MainWindow.xaml 的交互逻辑

///</summary>

publicpartialclassMainWindow : Window

{

public Excel.Application excel;

public Excel.Workbooks wbs;

public Excel.Workbook wb;

public Excel.Worksheets wss;

public Excel.Worksheet ws;

public MainWindow()

{

InitializeComponent();

}

privatevoid Button_Click(object sender, RoutedEventArgs e)

{

System.Windows.Forms.OpenFileDialog fd = new OpenFileDialog();

fd.Filter = @"Excel文件(*.xls; *.xlsx)|*.xls; *.xlsx|All File(*.*)|*.*";

if(fd.ShowDialog()==System.Windows.Forms.DialogResult.OK)

{

filePath.Text = fd.FileName;

}

}

privatevoid BtnAna_Click(object sender, RoutedEventArgs e)

{

try

{

excel= new Excel.Application();

wbs = excel.Workbooks;

wb = wbs.Add(filePath.Text);

int ncount = wb.Worksheets.Count;

if (ncount < 1)

{

System.Windows.Forms.MessageBox.Show("没有内容");

return;

}

else

{

// System.Windows.Forms.MessageBox.Show("ok,继续");

}

for(int i=0; i<ncount;i++)

{

ws = (Excel.Worksheet)wb.Sheets.Item[i+1];

cbSheet.Items.Add(ws.Name);

}

cbSheet.Text = ws.Name;

for(int i=0;i<10;i++)

{

cbLeft.Items.Add(i+1);

cbLeftNum.Items.Add(i + 1);

cbRight.Items.Add(i + 1);

cbRightNum.Items.Add(i + 1);

}

cbLeft.Text = "2";

cbLeftNum.Text = "3";

cbRight.Text = "4";

cbRightNum.Text = "5";

// excel.Quit();

// excel = null;

}

catch(Exception ex)

{

System.Windows.Forms.MessageBox.Show("解析数据异常,请检查数据结构");

}

finally

{

excel.Quit();

excel = null;

}

}

privatevoid BtnRes_Click(object sender, RoutedEventArgs e)

{

try

{

int nMin = Convert.ToInt32(tbMin.Text);

int nMax = Convert.ToInt32(tbMax.Text);

excel = new Excel.Application();

wbs = excel.Workbooks;

wb = wbs.Add(filePath.Text);

int ncount = wb.Worksheets.Count;

if (ncount < 1)

{

System.Windows.Forms.MessageBox.Show("没有内容");

return;

}

else

{

// System.Windows.Forms.MessageBox.Show("ok,继续");

}

ws = wb.Sheets[cbSheet.Text];

if(ws.Columns.Count<1)

{

System.Windows.Forms.MessageBox.Show("没有内容");

}

else

{

// System.Windows.Forms.MessageBox.Show("ok,继续");

}

string s1="01234";

int MaxIndex = 222;

string[,] lrData = newstring[MaxIndex, 4];

int leftArr = Convert.ToInt32(cbLeft.Text);

int leftNumArr = Convert.ToInt32(cbLeftNum.Text);

int rightArr = Convert.ToInt32(cbRight.Text);

int rightNumArr = Convert.ToInt32(cbRightNum.Text);

int leftNum = 0;

for(int i=1;i< (MaxIndex+1);i++)

{

if(((Excel.Range)ws.Cells[i, leftArr]).Text =="")

{

break;

}

else

{

lrData[leftNum, 0]= ((Excel.Range)ws.Cells[leftNum+1, leftArr]).Text;

lrData[leftNum, 1] = ((Excel.Range)ws.Cells[leftNum + 1, leftNumArr]).Text;

leftNum++;

}

}

int rightNum = 0;

for (int i = 1; i < (MaxIndex + 1); i++)

{

if (((Excel.Range)ws.Cells[i, rightArr]).Text == "")

{

break;

}

else

{

lrData[rightNum, 2] = ((Excel.Range)ws.Cells[rightNum + 1, rightArr]).Text;

lrData[rightNum, 3] = ((Excel.Range)ws.Cells[rightNum + 1, rightNumArr]).Text;

rightNum++;

}

}

Excel.Worksheet ws1 = (Excel.Worksheet)wb.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);

ws1.Name = "result";

int nl = 0;

int nr = 0;

for(int x=0;x<leftNum;x++)

{

for(int y=0;y<rightNum;y++)

{

ws1.Cells[x * rightNum + y + 1, 1] = lrData[x, 0];

ws1.Cells[x * rightNum + y + 1, 2] = lrData[x, 1];

ws1.Cells[x * rightNum + y + 1, 3] = lrData[y, 2];

ws1.Cells[x * rightNum + y + 1, 4] = lrData[y, 3];

ws1.Cells[x * rightNum + y + 1, 5] = lrData[x, 0] + lrData[y, 2];

ws1.Cells[x * rightNum + y + 1, 6] = lrData[x, 1] + "+" + lrData[y, 3];

nl = Convert.ToInt32(lrData[x, 1]);

nr = Convert.ToInt32(lrData[y, 3]);

ws1.Cells[x * rightNum + y + 1, 7] = Convert.ToString(nl + nr);

}

}

wb.SaveAs(cbSheet.Text+".xlsx");

wb.Save();

wb.Close();

System.Windows.Forms.MessageBox.Show("结果文件保存在 我的文档 中,命名为:"+ cbSheet.Text + ".xlsx");

}

catch(Exception ex)

{

System.Windows.Forms.MessageBox.Show("解析数据异常,请检查数据结构");

}

finally

{

excel.Quit();

excel = null;

}

}

}

}

您可能还会对下面的文章感兴趣: