Gembox.Spreadsheet 自动调整不适用于前两列 C#
Gembox.Spreadsheet autofit not working with first two columns C#
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 中的一系列单元格中。我当前的问题是,使用 Autofit 设置,我在单元格 2-5 上获得了正确的样式,但在 0 或 1 上却没有。将 Autofit(1) 更改为 AutoFit(0) 时,它正确地打印了第一个单元格,但是删除所有其他人。有人可以解释一下,或者让我了解如何使用自动格式打印所有单元格吗?
我发现该代码有几个问题:
- 对于
files
中的每个 file
,都会保存一个新的 XLS 文件,这很容易导致重写以前的 XLS 文件。
i
变量代表error
记录的索引,换句话说它代表行索引,所以用它来select一些[=35=没有意义] 这是自动安装的。
- 每
error
重写 table 的头部。
- 在第二次迭代中,table 的头部将覆盖先前写在 0 索引行上的第一个
error
。
- 等等
尽管如此,请尝试以下操作:
SpreadsheetInfo.SetLicense("License");
ExcelFile ef = new ExcelFile();
ExcelWorksheet ws = ef.Worksheets.Add("Error Spreadsheet info Elmah");
// Write header row.
ws.Cells[0, 0].Value = "Error ID";
ws.Cells[0, 1].Value = "Type";
ws.Cells[0, 2].Value = "Message";
ws.Cells[0, 3].Value = "Time";
ws.Cells[0, 4].Value = "Path Info";
ws.Cells[0, 5].Value = "HTTP Host";
int rowIndex = 1;
string folder = @"C:\ErrorsMay2017";
// I presume you want to write data from all XML files into a single spreadsheet.
foreach (string file in Directory.GetFiles(folder, "*.xml"))
{
// Get errors records.
var errors = from tagAttribute in XElement.Load(file).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()
};
// Write errors rows.
foreach (var error in errors)
{
ws.Cells[rowIndex, 0].Value = error.ErrorID;
ws.Cells[rowIndex, 1].Value = error.Type;
ws.Cells[rowIndex, 2].Value = error.Message;
ws.Cells[rowIndex, 3].Value = error.Time;
ws.Cells[rowIndex, 4].Value = error.PathInfo;
ws.Cells[rowIndex, 5].Value = error.HttpHost;
++rowIndex;
}
}
// AutoFit columns after writing all spreadsheet's data.
int columnCount = ws.CalculateMaxUsedColumns();
for (int columnIndex = 0; columnIndex < columnCount; ++columnIndex)
ws.Columns[columnIndex].AutoFit();
// Save to newer Excel format (.xlsx).
ef.Save(Path.Combine(folder,
string.Format("errorlog {0}.xlsx", DateTime.Now.ToString("MM-dd-yyyy-HH-mm"))));
希望对您有所帮助。
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 中的一系列单元格中。我当前的问题是,使用 Autofit 设置,我在单元格 2-5 上获得了正确的样式,但在 0 或 1 上却没有。将 Autofit(1) 更改为 AutoFit(0) 时,它正确地打印了第一个单元格,但是删除所有其他人。有人可以解释一下,或者让我了解如何使用自动格式打印所有单元格吗?
我发现该代码有几个问题:
- 对于
files
中的每个file
,都会保存一个新的 XLS 文件,这很容易导致重写以前的 XLS 文件。 i
变量代表error
记录的索引,换句话说它代表行索引,所以用它来select一些[=35=没有意义] 这是自动安装的。- 每
error
重写 table 的头部。 - 在第二次迭代中,table 的头部将覆盖先前写在 0 索引行上的第一个
error
。 - 等等
尽管如此,请尝试以下操作:
SpreadsheetInfo.SetLicense("License");
ExcelFile ef = new ExcelFile();
ExcelWorksheet ws = ef.Worksheets.Add("Error Spreadsheet info Elmah");
// Write header row.
ws.Cells[0, 0].Value = "Error ID";
ws.Cells[0, 1].Value = "Type";
ws.Cells[0, 2].Value = "Message";
ws.Cells[0, 3].Value = "Time";
ws.Cells[0, 4].Value = "Path Info";
ws.Cells[0, 5].Value = "HTTP Host";
int rowIndex = 1;
string folder = @"C:\ErrorsMay2017";
// I presume you want to write data from all XML files into a single spreadsheet.
foreach (string file in Directory.GetFiles(folder, "*.xml"))
{
// Get errors records.
var errors = from tagAttribute in XElement.Load(file).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()
};
// Write errors rows.
foreach (var error in errors)
{
ws.Cells[rowIndex, 0].Value = error.ErrorID;
ws.Cells[rowIndex, 1].Value = error.Type;
ws.Cells[rowIndex, 2].Value = error.Message;
ws.Cells[rowIndex, 3].Value = error.Time;
ws.Cells[rowIndex, 4].Value = error.PathInfo;
ws.Cells[rowIndex, 5].Value = error.HttpHost;
++rowIndex;
}
}
// AutoFit columns after writing all spreadsheet's data.
int columnCount = ws.CalculateMaxUsedColumns();
for (int columnIndex = 0; columnIndex < columnCount; ++columnIndex)
ws.Columns[columnIndex].AutoFit();
// Save to newer Excel format (.xlsx).
ef.Save(Path.Combine(folder,
string.Format("errorlog {0}.xlsx", DateTime.Now.ToString("MM-dd-yyyy-HH-mm"))));
希望对您有所帮助。