[英]C# Exporting data of winforms application to excel
我不知道如何将Windows窗体应用程序中的数据导出到Excel电子表格。 单击浏览后,选择我想要的文件并检查ACE。 我想将文本框中显示的数据导出到Excel电子表格。
private void button1_Click(object sender, EventArgs e)
{
string filename = filenameTextBox.Text;
if (File.Exists(filename))
{
aceInformationTextBox.Text = GetAccessControlInformation(filename);
}
else
{
MessageBox.Show("Given file does not exist.", this.Text,
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
private string GetAccessControlInformation(string filename)
{
StringBuilder info = new StringBuilder();
info.AppendLine("ACE entries for the file \"" + filename + "\":");
info.AppendLine();
FileSecurity security = File.GetAccessControl(filename);
AuthorizationRuleCollection acl = security.GetAccessRules(true, true,
typeof(System.Security.Principal.NTAccount));
foreach (FileSystemAccessRule ace in acl)
{
string aceInfo = GetAceInformation(ace);
info.AppendLine(aceInfo);
}
return info.ToString();
}
private string GetAceInformation(FileSystemAccessRule ace)
{
StringBuilder info = new StringBuilder();
string line = string.Format("Account: {0}", ace.IdentityReference.Value);
info.AppendLine(line);
line = string.Format("Type: {0}", ace.AccessControlType);
info.AppendLine(line);
line = string.Format("Rights: {0}", ace.FileSystemRights);
info.AppendLine(line);
line = string.Format("Inherited ACE: {0}", ace.IsInherited);
info.AppendLine(line);
return info.ToString();
}
private void browseButton_Click(object sender, EventArgs e)
{
if (browseFileDialog.ShowDialog() == DialogResult.OK)
{
filenameTextBox.Text = browseFileDialog.FileName;
}
}
private void addSelfToAclButton_Click(object sender, EventArgs e)
{
string filename = filenameTextBox.Text;
if (File.Exists(filename))
{
AddSelfToAcl(filename);
aceInformationTextBox.Text = GetAccessControlInformation(filename);
}
else
{
MessageBox.Show("Given file does not exist.", this.Text,
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
private static void AddSelfToAcl(string filename)
{
// create a rule for self
WindowsIdentity self = System.Security.Principal.
WindowsIdentity.GetCurrent();
FileSystemAccessRule rule = new FileSystemAccessRule(
self.Name, FileSystemRights.FullControl,
AccessControlType.Allow);
// add the rule to the file's existing ACL list
FileSecurity security = File.GetAccessControl(filename);
AuthorizationRuleCollection acl = security.GetAccessRules(true, true,
typeof(System.Security.Principal.NTAccount));
security.AddAccessRule(rule);
// persist changes and update view
File.SetAccessControl(filename, security);
}
private void aceInformationTextBox_TextChanged(object sender, EventArgs e)
{
}
private void browseFileDialog_FileOk(object sender, CancelEventArgs e)
{
}
private void button1_Click_1(object sender, EventArgs e)
{
}
}
}
如我所见,您有3个主要选项:
导出为CSV文件。 如果您的数据不包含任何格式,这是最简单的选择。 尽管名称为“逗号分隔值”,但大多数CSV文件实际上都是制表符分隔的。 这使得它们非常容易生产。 您只需要输出值,在每个单元格之间放置一个制表符( \\t
),并在每行之后放置一个换行符( Environment.NewLine
)。
使用OpenXML SDK导出到Excel。 这仅适用于Excel 2007-2013格式,但是这种格式已经存在了7年之久,现在已经广泛使用。 这是一个很好的入门资源: 使用Open XML SDK生成Excel 2010工作簿
使用Office Automation导出到Excel。 如果您的应用程序在ASP.NET下运行或作为非交互式Windows服务运行,则不能使用此方法,但是对于桌面和控制台应用程序来说,这是很好的选择。 另一个介绍性示例: 如何:使用COM Interop创建Excel电子表格
如果您确实需要Excel文档的所有功能(格式,列宽等),我个人建议使用选项2。 但是,如果只需要数据,则选项1将非常快速地编写代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.