我需要在 c 中写一行,以便在读取后将第一行的第一个单元格存储在字符串中。
它是这样的: -
str=read(row[index]);
c Sharp 中的确切语句是什么?有什么帮助吗?
如果在您的 PC 或 visualstudio 文件夹中安装了 ms office,则在导入参考后,您可以使用 excel interop 首先在您的 c 驱动器中包含来自 office12 或 office14 文件夹的参考,这是代码
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();
}
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)
{
....