简体   繁体   中英

C# Exporting data of winforms application to excel

I have no clue on how to export the data of my data from the windows forms application to a excel spreadsheet. After i click browse, selected the file I want and check the ACE. I want to export the data shown in the textbox to a Excel spreadsheet.

        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)
    {

    }
}

}

As I see it, you have 3 main options:

  1. Export as a CSV file. This is the easiest option if your data doesn't contain any formatting. Despite the name 'comma separated values', most CSV files are actually tab-delimited. This makes them extremely easy to produce; you just need to output your values, placing a tab character ( \\t ) between each cell, and a newline ( Environment.NewLine ) after each row.

  2. Export to Excel using the OpenXML SDK. This is only suitable for the Excel 2007-2013 format, but this has been around for 7 years and is now widespread. Here's a good introductory resource: Generating Excel 2010 Workbooks by using the Open XML SDK

  3. Export to Excel using Office Automation. You can't use this method if your application is running under ASP.NET or as a non-interactive Windows service, but it's fine for desktop and console applications. Another introductory example: How to: Use COM Interop to Create an Excel Spreadsheet

I would personally advocate option 2 if you really need all the features of an Excel document (formatting, column widths, etc). If you just need the data, however, option 1 would be very quick to code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM