0

i already know how to save in excel using openfiledialog. but what i want to happen is that when i save another file, i want it to be saved in my existing file but in a different sheet. can you guys help me?

this is my code for saving

Public Sub saveExcelFile(ByVal FileName As String)
            Dim xls As New Excel.Application
            Dim sheet As Excel.Worksheet
            Dim i As Integer
            xls.Workbooks.Add()
            sheet = xls.ActiveWorkbook.ActiveSheet
            Dim row As Integer = 1
            Dim col As Integer = 1
            For Each item As ListViewItem In ListView1.Items
                    For i = 0 To item.SubItems.Count - 1
                            sheet.Cells(row, col) = item.SubItems(i).Text
                            col = col + 1
                    Next
                    row += 1
                    col = 1
            Next
            xls.ActiveWorkbook.SaveAs(FileName)
            xls.Workbooks.Close()
            xls.Quit()
    End Sub


Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
            Dim saveFileDialog1 As New SaveFileDialog
            saveFileDialog1.Filter = "Excel File|*.xlsx"
            saveFileDialog1.Title = "Save an Excel File"
            saveFileDialog1.ShowDialog()
            If saveFileDialog1.FileName <> "" Then
                    saveExcelFile(saveFileDialog1.FileName)
            End If
            MessageBox.Show("Record Saved!")
    End Sub
0

Use NPOI for exporting to excel. You can specify Work sheet using NPOI. See the documentation for more details

You can also use Officehelper

Edit:

Use this sample code for creating excel sheet using NPOI. It is only a sample code. You need to refer the documentation for more details.

List<OrderViewModel> orderViewModelList = SelectOrders();

            //Add Column title
            HSSFWorkbook hssfworkbook = new HSSFWorkbook();
            HSSFSheet sheet1 = hssfworkbook.CreateSheet("Sheet1");
            sheet1.CreateRow(0).CreateCell(0).SetCellValue("Order No");

            sheet1.CreateRow(0).CreateCell(5).SetCellValue("Price");


            //check for if device exist for saving
            if (orderViewModelList.Count() > 0)
            {
                //Convert to csv class type
                int i = 0;
                foreach (var item in orderViewModelList)
                {
                    HSSFRow enqRow = sheet1.CreateRow(i + 1);

                    enqRow.CreateCell(0).SetCellValue(item.OrderId);

                    enqRow.CreateCell(5).SetCellValue(( item.OrderAmount));
                    i++;
                }
            }

            MemoryStream file = new MemoryStream();
            hssfworkbook.Write(file);

            byte[] Data = file.GetBuffer();
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=OrderReport.xls");
            Response.Charset = "";
            Response.ContentType = "application/ms-excel";
            Response.BinaryWrite(Data);
            Response.End();

See this example also

If you want to append a new sheet to existing excel sheet refer this example

1
  • @SigridAnnOmila See the sample code for creating excel. If you want to use the existing file and add one more sheet to it, refer the last link
    – Prasanth
    Nov 28 '11 at 6:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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