0
using System;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using GemBox.Spreadsheet;
namespace ExcelSortingAutomation
{
public class Program
{
[STAThread]
public static void Main(string[] args)
{
SpreadsheetInfo.SetLicense("License");
ExcelFile ef = new ExcelFile();
ExcelWorksheet ws = ef.Worksheets.Add("Error Spreadsheet info Elmah");
int i = 0;
var checkTime = DateTime.Now;
var files = Directory.GetFiles("C:\\ErrorsMay2017", "*.xml");
foreach (string file in files)
{
var tagElement = XElement.Load(file);
var errors = from tagAttribute in tagElement.DescendantsAndSelf("error").OrderBy(x => x.Attribute("type"))
select new
{
errorID = (string)tagAttribute.Attribute("errorId"),
type = (string)tagAttribute.Attribute("type"),
message = (string)tagAttribute.Attribute("message"),
time = (string)tagAttribute.Attribute("time"),
PathInfo = tagAttribute.Elements("serverVariables").Descendants("item").Where(x => x.Attribute("name").Value == "PATH_INFO")
.Select(x => x.Descendants("value").First().Attribute("string").Value).SingleOrDefault(),
HttpHost = tagAttribute.Elements("serverVariables").Descendants("item").Where(x => x.Attribute("name").Value == "HTTP_HOST")
.Select(x => x.Descendants("value").First().Attribute("string").Value).SingleOrDefault()
};
int columnCount = ws.CalculateMaxUsedColumns();
foreach (var error in errors)
{
// generates table head
ws.Cells[0, 0].Value = "errorID";
ws.Cells[0, 1].Value = "type";
ws.Cells[0, 2].Value = "message";
ws.Cells[0, 3].Value = "time";
ws.Cells[0, 4].Value = "Http_Host";
ws.Cells[0, 5].Value = "Path_Info";
if (i < columnCount)
{
ws.Columns[i].AutoFit(1, ws.Rows[1], ws.Rows[ws.Rows.Count - 1]);
}
Console.Write(error);
ws.Cells[i, 0].Value = error.errorID;
ws.Cells[i, 1].Value = error.type;
ws.Cells[i, 2].Value = error.message;
ws.Cells[i, 3].Value = error.time;
ws.Cells[i, 4].Value = error.PathInfo;
ws.Cells[i, 5].Value = error.HttpHost;
i++;
}
ef.Save("C:\\ErrorsMay2017\\errorlog " + checkTime.ToString("MM-dd-yyyy-HH-mm") + ".xls");
}
}
}
}
在上面的例子中工作,我有一個解析ELMAH錯誤日誌,隔出了幾個關鍵組件,然後將它們打印成一系列細胞的一個簡單的控制檯應用程序在Gembox.spreadsheet中。我現在的問題是,隨着自動調整設置,我得到正確的造型對細胞的2-5,而不是0或1。當改變自動調整(1)自動調整(0)它打印正確的第一個單元格,但刪除了所有其他人。有人能解釋一下,或者向我提供有關如何使用自動格式打印所有單元的見解?Gembox.Spreadsheet自動調整功能無法與前兩列C#
謝謝馬里奧,我實際上試着寫外循環的表頭,但因爲我遍歷他們,但它並不能正確顯示。我非常感謝你的回答,而且它看起來很完美。 – jpears