Message-ID: <1695392240.9683.1711699063263.JavaMail.web05$@web05> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_9682_801110869.1711699063263" ------=_Part_9682_801110869.1711699063263 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
Table of Contents |
---|
=20
|
ExcelWriter's ExcelApplication approach provides you with full programma= tic control over the Excel file formats (XLS, XLSX, XLSM). This includes th= e ability to create and modify: charts, formulas, formatting, data validati= on, conditional formatting, worksheet protection, images and more! This tut= orial will show you how to create a new workbook and write a value to a cel= l.
Include the SoftArtisans.OfficeWriter.ExcelWriter namespace= in the code behind.
using SoftArtisans.OfficeWriter.ExcelWriter;=20
Imports SoftArtisans.OfficeWriter.ExcelWriter=20
Instantiate the ExcelAppli= cation object
ExcelApplication XLAPP =3D new ExcelApplication();=20
Dim XLAPP As New ExcelApplication()=20
Unlike the ExcelTemplate object, which represents a single file, the ExcelApplication works as a file generation engine. The ExcelApplication object ca= n be used to create, open, and save multiple workbooks.
=
Create a new workbook with ExcelApplication.Create(FileFormat)
Workbook WB =3D XLAPP.Create(ExcelApplication.FileFormat.Xlsx);=20
Dim WB As Workbook =3D XLAPP.Create(ExcelApplication.FileFormat.Xlsx= )=20
ExcelWriter has the ability to create Excel 2003 (XLS= ) files and Excel 2007 (XLSX) files, but cannot convert between formats. Th= e file format must be declared when the workbook is created and the file ex= tension of the output file must match when the file is saved.
ExcelAp= plication.Create(FileFormat) automatically creates the first worksheet in t= he workbook for you.
Access the first worksheet through the Workbook.Worksheets collection
Worksheet WKST =3D WB.Worksheets[0];=20
Dim WKST As Worksheet =3D WB.Worksheets(0)=20
You can access worksheets by name (e.g. "Sheet1") or by = index (shown above), but ExcelWriter will throw an exception if you attempt= to access a worksheet that does not exist.
= li>
Write the value from the web form into a cell
string value =3D DataValueBox.Text.Trim(); WKST.Cells[0,0].Value =3D value;=20
Dim value As String =3D DataValueBox.Text.Trim() WKST.Cells(0, 0).Value =3D value=20
It is important to note that ExcelWriter indices are all 0-indexed= , unlike Excel indices, which are 1-indexed. This is importing when working= with cells, rows, columns, and worksheet positions in ExcelWriter. You can= also reference cells by name; in this case, it would be "A1".
Save the workbook
XLAPP.Save(WB, Page.Response, "Output.xlsx", false);= =20
XLAPP.Save(WB, Page.Response, "Output.xlsx", False)= =20
ExcelApplication.Sa= ve has the same output options as ExcelTemplate: save to disk, save to = memory stream, stream back to the client inline, and stream back to the cli= ent as an attachment. In this case, we're streaming the workbook back to th= e client as an attachment. Remember that the file extension must match the = file format specified when the workbook was created.
&nb= sp;
Run the code. In the output file, you will see that the custom text =
from the form has been inserted into cell "A1".
using SoftArtisans.OfficeWriter.ExcelWriter; ... ExcelApplication XLAPP =3D new ExcelApplication(); Workbook WB =3D XLAPP.Create(ExcelApplication.FileFormat.Xlsx); Worksheet WKST =3D WB.Worksheets[0]; string value =3D DataValueBox.Text.Trim(); WKST.Cells[0,0].Value =3D value; XLAPP.Save(WB, Page.Response, "Output.xlsx", false);=20
Include SoftArtisans.OfficeWriter.ExcelWriter ... Dim XLAPP As New ExcelApplication() Dim WB As Workbook =3D XLAPP.Create(ExcelApplication.FileFormat.Xlsx) Dim WKST As Worksheet =3D WB.Worksheets(0) Dim value As String =3D DataValueBox.Text.Trim() WKST.Cells(0, 0).Value =3D value XLAPP.Save(WB, Page.Response, "Output.xlsx", False)=20
You can download the code for the _Hello World_ tutorial as a Visual Stu= dio solution.