1

I have generated the excel file using C# coding as shown below

C# Coding:

protected void Page_Load(object sender, EventArgs e)
        {
            var doc = new SpreadsheetDocument(@"D:\JOSEPH\GenerateExcelSheet\OpenXmlPackaging.xlsx");
            Worksheet sheet1 = doc.Worksheets.Add("My Sheet");
            sheet1.Cells[1, 1].Value = "Test";

            sheet1.Cells["A1"].Value = 1;
            sheet1.Cells["C3"].Style = new OpenXmlPackaging.Style {

                Borders = new Borders(BorderStyles.Thick),
                Font = new Font
                {
                    Name = "Consolas",
                    Size = 10,
                    Style = FontStyles.DoubleUnderline | FontStyles.Bold
                },
                NumberFormat = new NumberFormat("#,##0.0;[Red](#,##0.0)"),
                Alignment = new Alignment
                {
                    HorizontalAlignment = HorizontalAlignment.Right,
                    VerticalAlignment = VerticalAlignment.Top,
                    WrapText = true,
                    Rotation = 45
                },
            };
            sheet1.Cells.CreateRange("B10:D5").MergeCells();
            sheet1.AutoFitColumns();
            sheet1.SetColumnWidth(3, 12);


        }

but when i tried to open the excel file, a message box appears as " enter image description here"

6
  • 1
    Just recommendation: Next time crop the message box instead of uploading whole screenshot so it's easier to read ;) Btw in what version of Excel are you opening it? Commented Aug 19, 2014 at 9:19
  • What happens here if 2 people call the page at the same time? Madness ensues! Commented Aug 19, 2014 at 9:22
  • Ok sure... @Ms.Nobody Commented Aug 19, 2014 at 9:23
  • MS Excel 2010 @Ms.Nobody Commented Aug 19, 2014 at 9:26
  • @DavidG: Absolutely - reason why I added my answer where Microsoft simply say don't do it! Commented Aug 19, 2014 at 9:50

2 Answers 2

2

The short answer is that you shouldn't be trying to create an Excel file on the server like this. There are technical issues and licensing issues of using the Excel API to do this - you can read more here http://support.microsoft.com/default.aspx/kb/257757

The technical problems may well explain why you're file is corrupt.

The better approach would be to use a library that doesn't rely on the Excel API, I listed a few in my question/answer here Reading Excel Files as a Server Process that may help you.

Sign up to request clarification or add additional context in comments.

1 Comment

@JoeMike: I only summarized the ones I was able to investigate and try at the time. Outside of that list I don't know I'm afraid.
0

Use this for open Excel:

     ApplicationClass excel = new ApplicationClass();
            Worksheet sheet1  = Activate(excel);
//insert you'r code

Use this for save and close Excel:

 excel.Visible = true;
object misValue = System.Reflection.Missing.Value;

    sheet1.SaveAs(filename, XlFileFormat.xlWorkbookDefault, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue);
                    excel.Application.Quit();
                    excel.Quit();

                    Marshal.ReleaseComObject(ws);
                    Marshal.ReleaseComObject(excel);
                    excel = null;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.