1

I have created an existing excel with two worksheet. For my first worksheet it consist of data and I would like to add another data into same excel but second worksheet. Can I know why my code doesn't work?

    public byte[] ExportToExcel()
            {
                Warning[] warnings = null;
                string[] streamids = null;
                string mimeType = null;
                string encoding = null;
                string extension = null;
                byte[] content;
                content = this.ReportViewer.LocalReport.Render("Excel", null, out mimeType, out encoding, out extension, out streamids, out warnings);
    
                return content;
            }

    ReportContent = ExportToExcel();

    var path = ScheduledService.gstrReportPath;
    var fullpath = Path.Combine(path, "ServerReport" + "_" + "_" + DateTime.Now.ToString("yyMMdd") + ".xls");
            
        bool exists = System.IO.Directory.Exists(path);
        if (!exists)
           System.IO.Directory.CreateDirectory(path);
        
//Problem --------------------------------------------------------------------    
        FileStream file = File.Open(fullpath, FileMode.Open, FileAccess.Write, FileShare.ReadWrite);
        file.Write(ReportContent, 0, ReportContent.Length);
        file.Flush();
        file.Close();
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Alexander Yap
  • 58
  • 1
  • 9
  • Where are you setting up the ReportDataSources for the ReportViewer? Looks like ReportViewer is a property so perhaps elsewhere in your code. Elaborate on what is not working. Are you producing the excel file at all? Is your question how do you write data to a 2nd worksheet in the same workbook? – iCode Dec 11 '15 at 02:24
  • Hello, I want to add my new data into new worksheet from an existing excel. The problem that I faced was the new data will overwrite my old data in sheet 1 and not add the new data into sheet 2. Thank you. – Alexander Yap Dec 11 '15 at 02:28

1 Answers1

0

Perhaps this post helps. I found similar comments on StackOverflow too. Set a property PageBreakAtEnd = true on appropriate object such as a rectangle or tablix. This will create a new Worksheet.

Community
  • 1
  • 1
iCode
  • 1,254
  • 1
  • 13
  • 16
  • Thank your information, but I want to add data into new worksheet instead of creating a worksheet, Thank you. – Alexander Yap Dec 11 '15 at 02:59
  • I'm not sure I understand. An Excel workbook (*.xls in your case) has 3 Worksheets (tabs). Your report is putting all data on the first tab I assume. Do you want some/all the data on the second Worksheet (tab)? Or are you talking about adding another Worksheet? Just want to understand what you are trying to achieve. – iCode Dec 11 '15 at 03:08
  • I want to add new on second Worksheet which mean Sheet 2 because my sheet 1 already got data but my sheet 2 is blank – Alexander Yap Dec 11 '15 at 03:27