-1

The Windows form app (C#) that we've already got imports files based on an .xlsx file. I've been tasked with writing a new section that automates the production of that spreadsheet. Creating the data for it is easy (and done). I've got any extra information I need to create the spreadsheet, except...

They told me at the 11th hour that Microsoft Office isn't installed on the server, so Microsoft.Office.Interop, etc., isn't a solution. I found out at the same time that the goal is to do this with minimal impact... so if I can avoid adding any references, that would be preferable; it doesn't sound like installing anything on the server is an option.

I was able to parse it out well enough into a .csv file, but it turns out the code that selects the file only looks for .xlsx files. So even though I can open it in Excel no problem, this doesn't actually provide a solution.

So, for all my research, I haven't found an answer to the question "Can I build an .xlsx file manually?" The closest I think I got was actually using an oledb, but since the spreadsheet doesn't exist out of the gate, every time I got to conn.open(); in the code below, it failed.

Are there any simple ways for me to create this file myself? I know .xlsx is basically compressed XML, but I can't find any reference where I can see what tags I would need to put inside a file and then how to properly compress it?

string cnStr = "Provider = Microsoft.ACE.OLEDB.12.0; Data Source = " +     filePathAndName + ";Persist Security Info=True;";
            using (System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(cnStr))
            {
                conn.Open();
                System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand();
                cmd.CommandText = "CREATE TABLE [" + worksheetCategory + "] (SKU VARCHAR, FileName VARCHAR);";
                cmd.ExecuteNonQuery();

                foreach (string f in files)
                {
                    string shortProductName = f.Substring(f.LastIndexOf("\\") + 1);
                    shortProductName = shortProductName.Substring(0, shortProductName.IndexOf("_"));
                    cmd.CommandText = "INSERT INTO [" + worksheetCategory + "](SKU, FileName) VALUES(" + shortProductName + "," + f + ");";
                    cmd.ExecuteNonQuery();
                }
                conn.Close();
            }
3
  • 1
    It's not. Believe me, this site is my go-to for problem solving. But that thread, and several others, have answers that are all either office.interop, or other installed 3rd party references. What I'm asking for is a way to use c# (linqToExcel?) and create the xlsx. Creating a csv is possible, maybe a conversion method would be helpful?
    – Jon
    Commented Apr 18, 2016 at 17:37
  • One of the answers discusses OLEDB, which is not third party. One offers a link to the documentation for the Excel XML file format, with an additional link to sample code. It's a duplicate, with answers providing many possible solutions to the exact same question.
    – Ken White
    Commented Apr 18, 2016 at 17:41
  • BTW, The office interop classes are not supported when running from a non interactive environment (from inside IIS or from windows services) so you really should not be using them on a server anyway even if Excel was installed. Commented Apr 18, 2016 at 17:54

2 Answers 2

1

I don't know if you consider this 3rd party or not because it is realsed by Microsoft but the Open XML SDK for Office will let you read and write xlsx files without having Excel installed on the machine.

Just use the NuGet package DocumentFormat.OpenXml to add it to your project, nothing needs to be "installed" with it, just make sure you include the needed dll files in your deployment.

1

You can use ado.net with OLEDB excel connection string.

However, it's for very basic reading and writing.

for my last project, I've used OpenXml 2.5, which is free and you can download it from microsoft. OpenXml will give you full control of excel. however, there's a learning curve.

Also, you dont want to use com interop. it's not optimal for concurrent exports and it always screws up because of resource deallocation issues.

Not the answer you're looking for? Browse other questions tagged or ask your own question.