在Excel电子表格中显示值,而不是在控制台、Linq到xml、c#中显示值
本文关键字:显示 Linq 控制台 xml Excel 电子表格 | 更新日期: 2023-09-27 18:08:11
我有这个从XML文件中解析值的工作代码。如何将数据写入Excel电子表格,而不是将数据写入控制台?请帮忙。
namespace TestCFG
{
class Program
{
public class XAxisCalib
{
public int Max1 { get; set; }
public int Min2 { get; set; }
public int Max3 { get; set; }
public int Min4 { get; set; }
public int Max5 { get; set; }
public int Min6 { get; set; }
}
static void Main(string[] args)
{
string[] fileEntries = Directory.GetFiles(@"c:'Sciclone UAC", "*.cfg*");
foreach (string fileName in fileEntries)
{
XDocument doc = XDocument.Load(fileName);
var query = from x in doc.Descendants("XAxisCalib")
select new
{
//Max1 = x.Attribute("Max").Value,
//Min2 = x.Attribute("Min").Value
MaxChild = x.Descendants("Max"),
MinChild = x.Descendants("Min")
};
foreach (var x in query)
{
foreach (var nextLevel in x.MaxChild)
{
Console.WriteLine("XMax: " + nextLevel.Value);
}
foreach (var nextLevel in x.MinChild)
{
Console.WriteLine("XMin: " + nextLevel.Value);
}
//Console.WriteLine("XAxisCalib");
}
var query2 = from y in doc.Descendants("YAxisCalib")
select new
{
//Max3 = x.Attribute("Max").Value,
//Min4 = x.Attribute("Min").Value
MaxChild = y.Descendants("Max"),
MinChild = y.Descendants("Min")
};
foreach (var y in query2)
{
foreach (var nextLevel in y.MaxChild)
{
Console.WriteLine("YMax: " + nextLevel.Value);
}
foreach (var nextLevel in y.MinChild)
{
Console.WriteLine("YMin: " + nextLevel.Value);
}
//Console.WriteLine("YAxisCalib");
var query3 = from z in doc.Descendants("ZAxisCalib")
select new
{
//Max5 = x.Attribute("Max").Value,
//Min6 = x.Attribute("Min").Value
MaxChild = z.Descendants("Max"),
MinChild = z.Descendants("Min")
};
foreach (var z in query3)
{
foreach (var nextLevel in z.MaxChild)
{
Console.WriteLine("ZMax: " + nextLevel.Value);
}
foreach (var nextLevel in z.MinChild)
{
Console.WriteLine("ZMin: " + nextLevel.Value);
}
//Console.WriteLine("ZAxisCalib");
}
}
}
}
}
}
使用office API比如
using System;
using System.Reflection;
using Microsoft.Office.Interop.Excel;
public class CreateExcelWorksheet
{
static void Main()
{
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
{
Console.WriteLine("EXCEL could not be started. Check that your office installation and project references are correct.");
return;
}
xlApp.Visible = true;
Workbook wb = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Worksheet ws = (Worksheet)wb.Worksheets[1];
if (ws == null)
{
Console.WriteLine("Worksheet could not be created. Check that your office installation and project references are correct.");
}
// Select the Excel cells, in the range c1 to c7 in the worksheet.
Range aRange = ws.get_Range("C1", "C7");
if (aRange == null)
{
Console.WriteLine("Could not get a range. Check to be sure you have the correct versions of the office DLLs.");
}
// Fill the cells in the C1 to C7 range of the worksheet with the number 6.
Object[] args = new Object[1];
args[0] = 6;
aRange.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, aRange, args);
// Change the cells in the C1 to C7 range of the worksheet to the number 8.
aRange.Value2 = 8;
}
}
从有几种方法可以从XML创建电子表格。—使用Office API。这是一个很好的方法,虽然有些沉重。这个API非常复杂,对于简单的操作来说是多余的,但如果需要公式则是必要的。-写出Excel XML,甚至更复杂。-写出一个CSV文件,有利于简单,非格式化输出。注意带有逗号等的值。-写一个HTML表格,Excel会打开所有单元格
使用Json。. NET,我相信你可以这样做:
string json = JsonConvert.SerializeObject(table, Formatting.Indented);
这是一个Json链接。NET:http://json.codeplex.com/