Excel Save to Stream in C#, VB.NET

  • Demo
  • C# source
  • VB.Net source
 

The sample demonstrates how to save an excel workbook to stream.

SaveStream.gif

		private void ExcelDocViewer( string fileName )
		{
			try
			{
				System.Diagnostics.Process.Start(fileName);
			}
			catch{}
		}

		private void btnRun_Click(object sender, System.EventArgs e)
		{
			Workbook workbook = new Workbook();
			Worksheet sheet = workbook.Worksheets[0];

			sheet.Range["C10"].Text = "The sample demonstrates how to save an excel workbook to stream.";

			//Save an excel workbook to stream
			FileStream fileStream = new FileStream("Sample.xls",FileMode.Create);
			workbook.SaveToStream(fileStream);

			fileStream.Close();

			ExcelDocViewer(Path.Combine(Application.StartupPath,"Sample.xls"));
		}

		Private Sub ExcelDocViewer(ByVal fileName As String)
			Try
				System.Diagnostics.Process.Start(fileName)
			Catch
			End Try
		End Sub

		Private Sub btnRun_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRun.Click
			Dim workbook As Workbook = New Workbook()
			Dim sheet As Worksheet = workbook.Worksheets(0)

			sheet.Range("C10").Text = "The sample demonstrates how to save an excel workbook to stream."

			'Save an excel workbook to stream
			Dim fileStream As FileStream = New FileStream("Sample.xls",FileMode.Create)
			workbook.SaveToStream(fileStream)

			fileStream.Close()

			ExcelDocViewer(Path.Combine(Application.StartupPath,"Sample.xls"))
		End Sub