我正在尝试做的事情:
我正在读取 Excel 工作表中的单元格值,并且文本通常具有特殊格式,例如上标和下标。我想在将它带入我的 C# 应用程序时保留这种格式,使用各种标签等的字符串。但是,在我当前的实现中,所有上标和下标字符都会丢失该格式。我相信这与我在整个过程中将事物转换为不同类型的事实有关:
const string fileName = "C:\\Users\\J.Smith\\Desktop\\FeatureInfoPropertyAttributesEXCEL.xlsx";
// Prepare the required items
Workbook wb = null;
// Start Excel
Application excel = new Application {Visible = false};
try
{
// Open file
Workbook wb = excel.Workbooks.Open(fileName);
// Read sheets
Sheets sheets = wb.Worksheets;
// Select sheet
Worksheet ws = (Worksheet) sheets.Item["FeatureInfoPropertyAttributes"];
string firstCellForPropertiesRange = "A2";
string secondCellForPropertiesRange = ws.Range[firstCellForPropertiesRange].End[XlDirection.xlDown].Address;
string firstCellForHeadersRange = firstCellForPropertiesRange.Replace("A", "B");
string secondCellForHeadersRange = secondCellForPropertiesRange.Replace("A", "B");
string firstCellForProposedHeadersRange1 = firstCellForHeadersRange.Replace("B", "C");
string secondCellForProposedHeadersRange1 = secondCellForHeadersRange.Replace("B", "C");
string firstCellForProposedHeadersRange2 = firstCellForProposedHeadersRange1.Replace("C", "D");
string secondCellForProposedHeadersRange2 = secondCellForProposedHeadersRange1.Replace("C", "D");
Range propertiesRange = ws.Range[firstCellForPropertiesRange, secondCellForPropertiesRange];
Range headersRange = ws.Range[firstCellForHeadersRange, secondCellForHeadersRange];
Range proposedHeadersRange1 = ws.Range[firstCellForProposedHeadersRange1, secondCellForProposedHeadersRange1];
Range proposedHeadersRange2 =
ws.Range[firstCellForProposedHeadersRange2, secondCellForProposedHeadersRange2];
List<string> properties =
propertiesRange.Cells.Cast<object>()
.Select((t, i) => ((Range) propertiesRange.Cells[i + 1]).Value2 ?? string.Empty)
.Cast<string>()
.ToList();
List<string> existingHeaders =
headersRange.Cells.Cast<object>()
.Select((t, i) => ((Range) headersRange.Cells[i + 1]).Value2 ?? string.Empty)
.Cast<string>()
.ToList();
List<string> proposedHeaders1 =
proposedHeadersRange1.Cells.Cast<object>()
.Select(
(t, i) =>
((Range) proposedHeadersRange1.Cells[i + 1]).Value2 ?? string.Empty)
.Cast<string>()
.ToList();
List<string> proposedHeaders2 =
proposedHeadersRange2.Cells.Cast<object>()
.Select(
(t, i) =>
((Range)proposedHeadersRange2.Cells[i + 1]).Value2 ?? string.Empty)
.Cast<string>()
.ToList();
foreach (string s in proposedHeaders1.Where(s => !s.Equals(string.Empty)))
{
Console.WriteLine(s);
}
foreach (string s in proposedHeaders2.Where(s => !s.Equals(string.Empty)))
{
Console.WriteLine(s);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
excel.Visible = true;
wb.Close(false, null, null);
excel.Quit();
}
第一个问题:
C#字符串是否支持字符串中的上标和下标等格式特征?
第二个问题:
如果第一个问题的答案是“是”,我将如何实现这一目标?