您的位置:首页 > 编程语言 > C#

C#读取excel数据

2017-03-10 15:22 369 查看
C#读取和写入excel数据
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Reflection;
using Microsoft.Office.Interop.Excel;

namespace test1
{
class Program
{
public static Array ReadXls(string filename, int index)//读取第index个sheet的数据
{
//启动Excel应用程序
Microsoft.Office.Interop.Excel.Application xls = new Microsoft.Office.Interop.Excel.Application();
//打开filename表
_Workbook book = xls.Workbooks.Open(filename, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

_Worksheet sheet;//定义sheet变量
xls.Visible = false;//设置Excel后台运行
xls.DisplayAlerts = false;//设置不显示确认修改提示

try
{
sheet = (_Worksheet)book.Worksheets.get_Item(index);//获得第index个sheet,准备读取
}
catch (Exception ex)//不存在就退出
{
Console.WriteLine(ex.Message);
return null;
}
Console.WriteLine(sheet.Name);
int row = sheet.UsedRange.Rows.Count;//获取不为空的行数
int col = sheet.UsedRange.Columns.Count;//获取不为空的列数

// Array value = (Array)sheet.get_Range(sheet.Cells[1, 1], sheet.Cells[row, col]).Cells.Value2;//获得区域数据赋值给Array数组,方便读取

Microsoft.Office.Interop.Excel.Range range = sheet.Range[sheet.Cells[1,1],sheet.Cells[row,col]];
Array value = (Array)range.Value2;

book.Save();//保存
book.Close(false, Missing.Value, Missing.Value);//关闭打开的表
xls.Quit();//Excel程序退出
//sheet,book,xls设置为null,防止内存泄露
sheet = null;
book = null;
xls = null;
GC.Collect();//系统回收资源
return value;
}

static void Main(string[] args)
{
string Current;
Current = Directory.GetCurrentDirectory();//获取当前根目录
Array Data = ReadXls(Current + "\\test3.xlsx", 1);//读取test.xlsx的第一个sheet表
foreach (string temp in Data)
Console.WriteLine(temp);
Console.ReadKey();
}

/*--------------------------------------------------------将数据写入excel------------------------------------------------------------------
public static bool WriteXls(string filename,string a)
{
//启动Excel应用程序
Microsoft.Office.Interop.Excel.Application xlsx = new Microsoft.Office.Interop.Excel.Application();
_Workbook book = xlsx.Workbooks.Add(Missing.Value); //创建一张表,一张表可以包含多个sheet

//如果表已经存在,可以用下面的命令打开
//_Workbook book = xls.Workbooks.Open(filename, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

_Worksheet sheet;//定义sheet变量
xlsx.Visible = false;//设置Excel后台运行
xlsx.DisplayAlerts = false;//设置不显示确认修改提示

for (int i = 1; i < 4; i++)//循环创建并写入数据到sheet
{
try
{
sheet = (_Worksheet)book.Worksheets.get_Item(i);//获得第i个sheet,准备写入
}
catch (Exception ex)//不存在就增加一个sheet
{
sheet = (_Worksheet)book.Worksheets.Add(Missing.Value, book.Worksheets[book.Sheets.Count], 1, Missing.Value);
}
sheet.Name = "第" + i.ToString() + "页";//设置当前sheet的Name
for (int row = 1; row < 20; row++)//循环设置每个单元格的值
{
for (int offset = 1; offset < 10; offset++)
sheet.Cells[row, offset] = "( " + row.ToString() + "," + offset.ToString() + " )"+a;

}
}
//将表另存为
book.SaveAs(filename, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

//如果表已经存在,直接用下面的命令保存即可
//book.Save();

book.Close(false, Missing.Value, Missing.Value);//关闭打开的表
xlsx.Quit();//Excel程序退出
//sheet,book,xls设置为null,防止内存泄露
sheet = null;
book = null;
xlsx = null;
GC.Collect();//系统回收资源
return true;
}
static void Main(string[] args)
{
string Current,zqh="zqh";
Current = Directory.GetCurrentDirectory();//获取当前根目录
WriteXls(Current + "\\test.xlsx",zqh);
}
*/

/*--------------------------------------------------------------------将数据写入text-------------------------------------------------------------------------
static void Main(string[] args)
{

StreamWriter sw = new StreamWriter("f:\\C#\\temp4.txt", true, Encoding.GetEncoding("gb2312"));
string data = "hello world";
sw.WriteLine(data);
int[] a = {1,2,3,4,5,6};
sw.WriteLine(a.Length);
for(int i=0;i<a.Length;i++)
{
sw.WriteLine(a[i]);
}
//sw.Close();
while (true)
{
string email, email0 = "请输入你的电子邮箱";
Console.WriteLine(email0);
email = Console.ReadLine();
sw.WriteLine(email0);
sw.WriteLine(email);
Console.WriteLine("你的邮箱是{0}", email);
Console.WriteLine("继续输入邮箱吗?");
string input = Console.ReadLine();
sw.WriteLine(input);
if (input.ToUpper() == "YES") continue;
else break;
}

sw.Close();
}
*/
}
}

引用好Microsoft.Office.Interop.Excel可以直接读取,

原来用的这句话:

// Array value = (Array)sheet.get_Range(sheet.Cells[1, 1], sheet.Cells[row, col]).Cells.Value2;//获得区域数据赋值给Array数组,方便读取
一直报错,说object未包含get_range定义,后来改成下面的语句,可以了

Microsoft.Office.Interop.Excel.Range range = sheet.Range[sheet.Cells[1,1],sheet.Cells[row,col]];
Array value = (Array)range.Value2;

还有一点:如果excel中的内容数据格式不同,可以用var类型,比如:

foreach (var temp in Data)
Console.WriteLine(temp);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: