c#将winforms应用程序的数据导出到excel
本文关键字:excel 数据 winforms 应用程序 | 更新日期: 2023-09-27 17:50:29
我不知道如何将我的数据从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自动化导出到Excel。如果应用程序在ASP下运行,则不能使用此方法。NET或作为非交互式的Windows服务,但它适用于桌面和控制台应用程序。另一个介绍性示例:如何使用COM互操作创建Excel电子表格
如果你真的需要Excel文档的所有功能(格式、列宽等),我个人建议选择2。但是,如果您只需要数据,则选项1将非常快速地编写代码。