在Excel中设置第一行的第一个单元格
本文关键字:一行 第一个 单元格 Excel 设置 | 更新日期: 2023-09-27 18:37:28
我需要用 c sharp 写一行,用于在读取后将第一行的第一个单元格存储在字符串中。
它像:-
str=read(row[index]);
C 夏普中的确切陈述是什么?有什么帮助吗?
private Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
private static string ReadSpreadsheet()
{
Workbook wb = null;
wb = excel.Workbooks.Open("C:'APathToExcelSpreadsheet.xls", false, true);
//Get the values in the sheet
Range rng = wb.ActiveSheet.UsedRange;
object[,] values;
if (rng.Value2.GetType().IsArray)
{
values = (object[,])rng.Value2;
}
else
{
values = new object[2, 2];
values[1, 1] = rng.Value2;
}
for (int row = 1; row <= values.GetUpperBound(0); row++)
{
for (int col = 1; col <= values.GetUpperBound(1); col++)
{
if (values[row, col] != null)
{
....
您可以使用Excel互操作来阅读它,方法是首先在C驱动器中包含其来自Office12或Office14文件夹的引用,如果MS Office安装在PC或导入引用后的VisualStudio文件夹中,这是代码
public static void GetExcelData(string _path)
{
Excel.Application xlApp = new Excel.ApplicationClass();
Excel._Workbook xlWorkbook = xlApp.Workbooks.Open(_path);
Excel._Worksheet xlWorksheet = (Excel.Worksheet)xlWorkbook.Sheets.get_Item(1);
Excel.Range xlRange = xlWorksheet.UsedRange;
string firstcell == (xlRange.Cells[1, 1] as Excel.Range).Value2.ToString();
xlWorkbook.Close(true, Type.Missing, Type.Missing);
xlApp.Quit();
}