c# 如何实现将excel中的数据读入一个string中 20
excel中的数据格式如图所示:前五行只有第一列有数据,从第六行开始,第一列和第二列都有数据希望实现的功能是:构造一个函数,将这个excel中的数据存入到一个string...
excel中的数据格式如图所示:前五行只有第一列有数据,从第六行开始,第一列和第二列都有数据
希望实现的功能是:构造一个函数,将这个excel中的数据存入到一个string(string名称为fs)中,数据与数据间以“,”隔开,类似于“1,1......... ”这种形式。
函数名为protected void readFile_XLS
还请c#高手多多指教 展开
希望实现的功能是:构造一个函数,将这个excel中的数据存入到一个string(string名称为fs)中,数据与数据间以“,”隔开,类似于“1,1......... ”这种形式。
函数名为protected void readFile_XLS
还请c#高手多多指教 展开
2个回答
展开全部
你要横着读还是竖着读?而且读的范围是不是整个表?,每行每列的数据是不是连续的?
追问
竖着读:相当于前五行直接取“1”,从第六行开始每行取两个“1”,数据是连续的,读整个表
竖着读:相当于前五行直接取“1”,从第六行开始每行取两个“1”,数据是连续的,读整个表
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Excel;
using System.Diagnostics;
using System.IO;
namespace OperateExcel
{
class Program
{
private Microsoft.Office.Interop.Excel._Application excelApp;
private string filePath = string.Empty;
private Microsoft.Office.Interop.Excel.WorkbookClass workBooks;
public Program(string filePath)
{
this.filePath = filePath;
excelApp = new Microsoft.Office.Interop.Excel.Application();
object obj = System.Reflection.Missing.Value;
workBooks = (Microsoft.Office.Interop.Excel.WorkbookClass)excelApp.Workbooks.Open(filePath,obj,false,obj,obj,obj,true,obj,obj,true,obj,obj,obj,obj,obj);
}
// get all the workbooks
public List<string> GetSheetNames()
{
List<string> list = new List<string>();
Microsoft.Office.Interop.Excel.Sheets sheets = workBooks.Worksheets;
//string sheetName = string.Empty;
foreach (Microsoft.Office.Interop.Excel.Worksheet sheet in sheets)
{
list.Add(sheet.Name);
}
return list;
}
// get each sheet
public Microsoft.Office.Interop.Excel.Worksheet GetWorksheetByName(string name)
{
Microsoft.Office.Interop.Excel.Worksheet sheet = null;
Microsoft.Office.Interop.Excel.Sheets sheets = workBooks.Worksheets;
foreach (Microsoft.Office.Interop.Excel.Worksheet s in sheets)
{
if (s.Name == name)
{
sheet = s;
break;
}
}
return sheet;
}
// get content of one cell in a sheet
public string GetContent(string sheetName,object rowindex, object columnindex)
{
string value = null;
Microsoft.Office.Interop.Excel.Worksheet sheet = GetWorksheetByName(sheetName);
//get value in A1
value = (string)((Range)sheet.Cells[rowindex ,columnindex]).Text;
return value;
}
public void Close()
{
excelApp.Quit();
excelApp = null;
}
static void Main(string[] args)
{
string filePath = @"D:\test.xls";
if (File.Exists(filePath))
{
Program p = new Program(filePath);
string value = p.GetContent("test", "1", "A");
Console.WriteLine("value of A1 is {0}", value);
p.Close();
}
else
Console.WriteLine("File doesn't exits");
}
}
}
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Excel;
using System.Diagnostics;
using System.IO;
namespace OperateExcel
{
class Program
{
private Microsoft.Office.Interop.Excel._Application excelApp;
private string filePath = string.Empty;
private Microsoft.Office.Interop.Excel.WorkbookClass workBooks;
public Program(string filePath)
{
this.filePath = filePath;
excelApp = new Microsoft.Office.Interop.Excel.Application();
object obj = System.Reflection.Missing.Value;
workBooks = (Microsoft.Office.Interop.Excel.WorkbookClass)excelApp.Workbooks.Open(filePath,obj,false,obj,obj,obj,true,obj,obj,true,obj,obj,obj,obj,obj);
}
// get all the workbooks
public List<string> GetSheetNames()
{
List<string> list = new List<string>();
Microsoft.Office.Interop.Excel.Sheets sheets = workBooks.Worksheets;
//string sheetName = string.Empty;
foreach (Microsoft.Office.Interop.Excel.Worksheet sheet in sheets)
{
list.Add(sheet.Name);
}
return list;
}
// get each sheet
public Microsoft.Office.Interop.Excel.Worksheet GetWorksheetByName(string name)
{
Microsoft.Office.Interop.Excel.Worksheet sheet = null;
Microsoft.Office.Interop.Excel.Sheets sheets = workBooks.Worksheets;
foreach (Microsoft.Office.Interop.Excel.Worksheet s in sheets)
{
if (s.Name == name)
{
sheet = s;
break;
}
}
return sheet;
}
// get content of one cell in a sheet
public string GetContent(string sheetName,object rowindex, object columnindex)
{
string value = null;
Microsoft.Office.Interop.Excel.Worksheet sheet = GetWorksheetByName(sheetName);
//get value in A1
value = (string)((Range)sheet.Cells[rowindex ,columnindex]).Text;
return value;
}
public void Close()
{
excelApp.Quit();
excelApp = null;
}
static void Main(string[] args)
{
string filePath = @"D:\test.xls";
if (File.Exists(filePath))
{
Program p = new Program(filePath);
string value = p.GetContent("test", "1", "A");
Console.WriteLine("value of A1 is {0}", value);
p.Close();
}
else
Console.WriteLine("File doesn't exits");
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询