【发布时间】:2017-09-12 10:41:28
【问题描述】:
这是我的 C# 代码,Visual Studio 2015。
我想保存这些 Excel 单元格以转换文本文件。
例如,从AN50中选择AN1中的值,然后保存
“Range1.txt”。
非常感谢。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
namespace WindowsFormsApplication4
{
static class Program
{
/// <summary>
[STAThread]
static void Main()
{
string testingExcel = @"V:\000.xlsx";
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Workbook xlWorkbook = xlApp.Workbooks.Open(testingExcel, Type.Missing, true);
_Worksheet xlWorksheet = (_Worksheet)xlWorkbook.Sheets[1];
//Range xlRange = xlWorksheet.UsedRange;
Range Range1 = xlWorksheet.get_Range("AN1","AN50");
foreach (Range a in Range1.Rows.Cells)
{
Console.WriteLine("Address: " + a.Address + " - Value: " + a.Value);
}
Range Range2 = xlWorksheet.get_Range("AO1", "AO50");
foreach (Range b in Range2.Rows.Cells)
{
Console.WriteLine("Address: " + b.Address + " - Value: " + b.Value);
}
xlWorkbook.Close();
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlWorkbook);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlApp);
}
}
}
【问题讨论】:
-
用代码而不是代码图像格式化问题
-
看到这个代码stackoverflow.com/questions/25332404/…你可以通过这样的代码修改你的excel文件
-
这是您尝试解决的另一个问题 - 当您有一堆提取的文本时如何写入文本文件。只需搜索该问题并结合您已有的内容