基本信息
源码名称:C# 读写excel表 例子源码下载
源码大小:0.15M
文件格式:.pdf
开发语言:C#
更新时间:2014-11-04
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
namespace MSDNEXCEL
{
public class Account
{
public int ID { get; set; }
public double Balance { get; set; }
}
class Program
{
static void DisplayInExcel(IEnumerable<Account> accounts)
{
var excelApp = new Excel.Application();
// Make the object visible.
excelApp.Visible = true;
// Create a new, empty workbook and add it to the collection returned
// by property Workbooks. The new workbook becomes the active workbook.
// Add has an optional parameter for specifying a praticular template.
// Because no argument is sent in this example, Add creates a new workbook.
excelApp.Workbooks.Add();
// This example uses a single workSheet. The explicit type casting is
// removed in a later procedure.
Excel._Worksheet workSheet = (Excel.Worksheet)excelApp.ActiveSheet;
workSheet.Cells[1, "A"] = "ID Number";
workSheet.Cells[1, "B"] = "Current Balance";
var row = 1;
foreach (var acct in accounts)
{
row ;
workSheet.Cells[row, "A"] = acct.ID;
workSheet.Cells[row, "B"] = acct.Balance;
}
workSheet.Columns[1].AutoFit();
workSheet.Columns[2].AutoFit();
}
static void Main(string[] args)
{
var bankAccounts = new List<Account>
{
new Account
{
ID = 345678,
Balance = 541.27
},
new Account
{
ID = 1230221,
Balance = -127.44
}
};
// DisplayInExcel(bankAccounts);
ReadExcel(@"C:\Users\Administrator.PC-20121127WIOX\Desktop\project\MSDNEXCEL\MSDNEXCEL\
bin\Debug\Book1.xls");
//@"C:\Users\Administrator.PC-20121127WIOX\Desktop\project\MSDNEXCEL\MSDNEXCEL\bin\Debu
g\Book1.xls"为写入部分中Excel的保存目录
Console.ReadLine();
}
static void ReadExcel(string str)
{
var excelAppRead = new Excel.Application();
excelAppRead.Application.Workbooks.Open(str);
Account temp ;
Excel._Worksheet workSheet = (Excel.Worksheet)excelAppRead.ActiveSheet;
var count = workSheet.Cells.Count;
for (int i = 2; i <4; i )
{
temp = new Account();
temp.ID = int.Parse(workSheet.Cells[i, "A"].Text);
temp.Balance = double.Parse(workSheet.Cells[i, "B"].Text);
Console.WriteLine("ID: {0}\t;Balance: {1}", temp.ID,temp.Balance);
}
}
}
}