Convert to PDF private void Celltopdf (String cellpath, String pdfpath) {Workbook book = new Workbook (); Book. Open (Cellpath); Book. Save (Pdfpath, fileformattype.pdf); }
Import
Private DataTable Readexcel (string fielpathname) {Workbook book = new Workbook (); Book. Open (Fielpathname); Worksheet sheet = Book. Worksheets[0]; Cells cells = sheet. Cells; The first row of headings does not import//cells. Exportdatatable (,);
、、
Table headings converted to using exportdatatableasstring are string types, supported, and DataTable. Select uses a DataTable Dtresult = cells. Exportdatatable (1, 0, cells. Maxdatarow, cells. Maxdatacolumn + 1, false); True takes the first row as the caption, and false C1,--CN as the title dtresult. columns["Column1"]. ColumnName = "Name"; Dtresult. columns["Column2"]. ColumnName = "Age"; Dtresult. columns["Column3"]. ColumnName = "Gender"; Dtresult. columns["Column4"]. ColumnName = "Salary"; Dtresult. columns["Column5"]. ColumnName = "BirthDay"; int r = Dtresult. Rows.Count; return dtresult; }
Export
MemoryStream stream = Outfiletostream (MODELDB.DBDT); byte[] bytes = stream. ToArray ();//streamtobytes (stream); Response.ContentType = "Application/octet-stream"; Notifies the browser to download the file instead of opening Response.AddHeader ("Content-disposition", "attachment; Filename= "+ httputility.urlencode (" 23.xls ", System.Text.Encoding.UTF8)); Response.BinaryWrite (bytes); Response.Flush (); Response.End ();
private MemoryStream outfiletostream (DataTable dt) { &N Bsp Workbook Workbook = new Workbook (); Workbooks Worksheet sheet = workbook. Worksheets[0]; Worksheets cells cells = sheet. cells;//cell //Set style for headings Aspose.Cells.Style styletitle = workbook. Styles[workbook. Styles.add ()];//new style [title] styletitle.horizontalalignment = textalignmenttype.center;//Text Center styleTitle.Font.Name = "Arial";//Text font styleTitle.Font.Size = 12;//Text size & nbsp StyleTitle.Font.IsBold = true;//Bold Styletitle.foregroundcolor = SYstem. Drawing.Color.FromArgb (153, 204, 0); Styletitle.pattern = Backgroundtype.solid ;//Set background color //normal string style AS Pose. Cells.style strstyle = workbook. Styles[workbook. Styles.add ()];//new style strstyle.horizontalalignment = textalignmenttype.center;//Text Center strStyle.Font.Name = "Arial";//Text font & nbsp strStyle.Font.Size = 9;//Text size STRSTYLE.F Ont. IsBold = true;//Bold strstyle.istextwrapped = true;//cell content Wrap & nbsp //Money style, Aspose.Cells.Style Moneystyle = Workbo Ok. Styles[workbook. Styles.add ()];//new style money Moneystyle.number = 4;//thousand-way display money //Date style Aspose. Cells.style datestyle = workbook. Styles[workbook. Styles.add ()]; Datestyle.custom = "Yyyy-mm-dd"; int colnum = dt. columns.count;//table Columns int Rownum = dt. rows.count;//table Rows //Column name lines //Data inventory headings are generally English, and Excel is generally Chinese. So the headline is generally added cells[0, 0]. Putvalue ("name _"); Cells[0, 0]. SetStyle (styletitle); cells[0, 1]. Putvalue ("Age _"); Cells[0, 1]. SetStyle (styletitle); cells[0, 2]. Putvalue ("Sex _"); Cells[0, 2]. SetStyle (styletitle); cells[0, 3]. Putvalue ("Wages _"); Cells[0, 3]. SetStyle (styletitle);   cells[0, 4]. Putvalue ("Date of birth _"); Cells[0, 4]. SetStyle (styletitle); cells. Setrowheight (0, +); int[] Moneycol = {3};//column miniature with thousands of decimal points int[] Datecol = {4};//requires a Date format column index //Generate columns &NBS P for (int i = 0; i < Rownum; i++) { &nbs P for (int k = 0; k < colnum; k++) &NB Sp { if (dt. ROWS[I][K] = null { &NBS P if (Moneycol.contains (k)) &NBsp { Cells[i + 1, K]. Putvalue (convert.todecimal (dt. ROWS[I][K]. ToString ())); cells[i 1 , K]. SetStyle (Moneystyle); } &N Bsp ELSE if (Datecol.contains (k)) &N Bsp { &N Bsp Cells[i + 1, K]. Putvalue (convert.todatetime (dt. ROWS[I][K]. ToString ())); cells[i 1 , K]. SetStyle (Datestyle); } else &NBSP ; { &N Bsp Cells[i + 1, K]. Putvalue (dt. ROWS[I][K]. ToString (), true); cells[ i + 1, K]. SetStyle (Strstyle); } &NBS P } }&NB Sp cells. Setrowheight (1 + i, +); } M Emorystream ms = Workbook. Savetostream (); return ms; }
<summary> //Export data to local //</SUMMARY>&NB sp; //<param name= "DT" > Data to be exported </param> //<p Aram Name= "tableName" > table title </param> //<param name= "path" > Save path </ param> public static void Outfiletodisk (DataTable dt, string tableName, string path) &N Bsp { Workbook Workbook = new Workbook ();//Workbook   ; Worksheet sheet = workbook. Worksheets[0]; Worksheets cells cells = sheet. cells;//cell //Set style for headings Aspose.Cells.Style styletitle = workbook. Styles[workbook. Styles.add ()];//new style &nbsP styletitle.horizontalalignment = textalignmenttype.center;//Text Center styleTitle.Font.Name = "Arial";//Text font StyleTitle.Font.Si Ze = 18;//Text size StyleTitle.Font.IsBold = true;//Bold //style 2 Aspose.Cells.Style style2 = workbook. Styles[workbook. Styles.add ()];//new style Style2. HorizontalAlignment = textalignmenttype.center;//Text Center Style2. Font.Name = "Arial";//Text font Style2. Font.Size = 14;//Text size Style2. Font.isbold = true;//Bold Style2. istextwrapped = true;//cell content wrap Style2. Borders[bordertype.leftborder]. LineStyle = cellbordertype.thin; Style2. Borders[bordertype.rightborder]. LineStyle = cellbordertype.thin; Style2. Borders[bordertype.topborder]. LineStyle = cellbordertype.thin; Style2. Borders[bordertype.bottomborder]. LineStyle = cellbordertype.thin; //style 3 &NB Sp Aspose.Cells.Style style3 = workbook. Styles[workbook. Styles.add ()];//new style Style3. HorizontalAlignment = textalignmenttype.center;//Text Center Style3. Font.Name = "Arial";//Text font Style3. Font.Size = 12;//Text size Style3. Borders[bordertype.leftborder]. LineStyle = cellbordertype.thin; StYle3. Borders[bordertype.rightborder]. LineStyle = cellbordertype.thin; Style3. Borders[bordertype.topborder]. LineStyle = cellbordertype.thin; Style3. Borders[bordertype.bottomborder]. LineStyle = cellbordertype.thin; int colnum = dt. columns.count;//table Columns int Rownum = dt. rows.count;//table Rows //Generate line 1 header line cells. Merge (0, 0, 1, colnum);//Merge Cells cells[0, 0]. Putvalue (tableName);//fill in content cells[0, 0]. SetStyle (styletitle); cells. Setrowheight (0, $); //Generate row 2 column name line &NB Sp for (int i = 0; i < colnum; i++) &NBsp { cells[1, I]. Putvalue (dt. Columns[i]. ColumnName); cells[1, I]. SetStyle (style2); cells. Setrowheight (1, +); } //Generate Data rows &nbs p; for (int i = 0; i < Rownum; i++) { for (int k = 0; k < colnum; k++) &NB Sp { cells[2 + I, K]. Putvalue (dt. ROWS[I][K]. ToString ()); cells[2 + I, K]. SetStyle (style3); }   cells. Setrowheight (2 + i, +); } workbook. Save (path); }
Aspose Export Excel Mini demo