Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write asp.net Export Excel Class Library Code

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "how to write asp.net export Excel class library code", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "how to write asp.net export Excel class library code"!

The copy code is as follows:

Using System

Using System.Collections.Generic

Using System.Reflection

Using System.Web

Using Excel = Microsoft.Office.Interop.Excel

/ / /

/ Summary description of ExcelClass

/ / /

Public class ExcelClass

{

/ / /

/ build the ExcelClass class

/ / /

Public ExcelClass ()

{

This.m_objExcel = new Excel.Application ()

}

/ / /

/ build the ExcelClass class

/ / /

/ Excel.Application

Public ExcelClass (Excel.Application objExcel)

{

This.m_objExcel = objExcel

}

/ / /

/ / column label

/ / /

Private string AList = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

/ / /

/ get the characters that describe the area

/ / /

/ / /

/ / /

/ / /

Public string GetAix (int x, int y)

{

Char [] AChars = AList.ToCharArray ()

If (x > = 26) {return ";}

String s = ""

S = s + AChars [x-1] .ToString ()

S = s + y.ToString ()

Return s

}

/ / /

/ / assign a value of 1 to the cell

/ / /

/ / Line number

/ / column number

/ / one alignment (CENTER, LEFT, RIGHT)

/ / value

Public void setValue (int y, int x, string align, string text)

{

Excel.Range range = sheet.get_Range (this.GetAix (x, y), miss)

Range.set_Value (miss, text)

If (align.ToUpper () = = "CENTER")

{

Range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter

}

If (align.ToUpper () = = "LEFT")

{

Range.HorizontalAlignment = Excel.XlHAlign.xlHAlignLeft

}

If (align.ToUpper () = = "RIGHT")

{

Range.HorizontalAlignment = Excel.XlHAlign.xlHAlignRight

}

}

/ / /

/ / assign a value to the cell 2

/ / /

/ / Line number

/ / column number

/ / value

Public void setValue (int y, int x, string text)

{

Excel.Range range = sheet.get_Range (this.GetAix (x, y), miss)

Range.set_Value (miss, text)

}

/ / /

/ / assign a value of 3 to the cell

/ / /

/ / Line number

/ / column number

/ / value

/ / character format

/ Color

Public void setValue (int y, int x, string text, System.Drawing.Font font, System.Drawing.Color color)

{

This.setValue (x, y, text)

Excel.Range range = sheet.get_Range (this.GetAix (x, y), miss)

Range.Font.Size = font.Size

Range.Font.Bold = font.Bold

Range.Font.Color = color

Range.Font.Name = font.Name

Range.Font.Italic = font.Italic

Range.Font.Underline = font.Underline

}

/ / /

/ / insert a new row

/ / /

/ / template line number

Public void insertRow (int y)

{

Excel.Range range = sheet.get_Range (GetAix (1, y), GetAix (25, y))

Range.Copy (miss)

Range.Insert (Excel.XlDirection.xlDown, miss)

Range.get_Range (GetAix (1, y), GetAix (25, y))

Range.Select ()

Sheet.Paste (miss, miss)

}

/ / /

/ / paste the cut into the current area

/ / /

Public void past ()

{

String s = "a _

Sheet.Paste (sheet.get_Range (this.GetAix (10,10), miss), s)

}

/ / /

/ set the border

/ / /

/ / /

/ / /

/ / /

/ / /

/ / /

Public void setBorder (int x1, int y1, int x2, int y2, int Width)

{

Excel.Range range = sheet.get_Range (this.GetAix (x1, y1), miss)

((Excel.Range) range.Cells [x1, y1]) .ColumnWidth = Width

}

Public void mergeCell (int x1, int y1, int x2, int y2)

{

Excel.Range range = sheet.get_Range (this.GetAix (x1, y1), this.GetAix (x2, y2))

Range.Merge (true)

}

Public Excel.Range getRange (int x1, int y1, int x2, int y2)

{

Excel.Range range = sheet.get_Range (this.GetAix (x1, y1), this.GetAix (x2, y2))

Return range

}

Private object miss = Missing.Value; / / ignored parameter OLENULL

An example of the application program of private Excel.Application Microsoft objExcel

Private Excel.Workbooks masked objBooks / worksheet collection

Private Excel.Workbook masked objBookbot / worksheet for the current operation

Table for the current operation of private Excel.Worksheet sheet;//

Public Excel.Worksheet CurrentSheet

{

Get

{

Return sheet

}

Set

{

This.sheet = value

}

}

Public Excel.Workbooks CurrentWorkBooks

{

Get

{

Return this.m_objBooks

}

Set

{

This.m_objBooks = value

}

}

Public Excel.Workbook CurrentWorkBook

{

Get

{

Return this.m_objBook

}

Set

{

This.m_objBook = value

}

}

/ / /

/ Open the Excel file

/ / /

/ / path

Public void OpenExcelFile (string filename)

{

UserControl (false)

M_objExcel.Workbooks.Open (filename, miss, miss

Miss, miss)

M_objBooks = (Excel.Workbooks) m_objExcel.Workbooks

M_objBook = m_objExcel.ActiveWorkbook

Sheet = (Excel.Worksheet) m_objBook.ActiveSheet

}

Public void UserControl (bool usercontrol)

{

If (m_objExcel = = null) {return;}

M_objExcel.UserControl = usercontrol

M_objExcel.DisplayAlerts = usercontrol

M_objExcel.Visible = usercontrol

}

Public void CreateExceFile ()

{

UserControl (false)

M_objBooks = (Excel.Workbooks) m_objExcel.Workbooks

M_objBook = (Excel.Workbook) (m_objBooks.Add (miss))

Sheet = (Excel.Worksheet) m_objBook.ActiveSheet

}

Public void SaveAs (string FileName)

{

M_objBook.SaveAs (FileName, miss, miss

Miss, Excel.XlSaveAsAccessMode.xlNoChange

Excel.XlSaveConflictResolution.xlLocalSessionChanges

Miss, miss)

/ / m_objBook.Close (false, miss, miss)

}

Public void ReleaseExcel ()

{

M_objExcel.Quit ()

System.Runtime.InteropServices.Marshal.ReleaseComObject ((object) m_objExcel)

System.Runtime.InteropServices.Marshal.ReleaseComObject ((object) m_objBooks)

System.Runtime.InteropServices.Marshal.ReleaseComObject ((object) m_objBook)

System.Runtime.InteropServices.Marshal.ReleaseComObject ((object) sheet)

M_objExcel = null

M_objBooks = null

M_objBook = null

Sheet = null

GC.Collect ()

}

Public bool KillAllExcelApp ()

{

Try

{

If (m_objExcel! = null) / / isRunning is the flag that determines how xlApp starts.

{

M_objExcel.Quit ()

System.Runtime.InteropServices.Marshal.ReleaseComObject (m_objExcel)

/ / to release the COM component is to subtract its reference count by 1.

/ / System.Diagnostics.Process theProc

Foreach (System.Diagnostics.Process theProc in System.Diagnostics.Process.GetProcessesByName ("EXCEL"))

{

/ / close the graphics window first. If the shutdown fails. Sometimes you can't see the excel of the graphics window in the state.

/ / but there is still an EXCEL.EXE process in the process, so you need to kill it: P

If (theProc.CloseMainWindow () = = false)

{

TheProc.Kill ()

}

}

M_objExcel = null

Return true

}

}

Catch

{

Return false

}

Return true

}

}

/ / /

/ / Click the print button event

/ / /

/ / /

/ / /

Protected void Sendbu_Click (object sender, EventArgs e)

{

Try

{

/ / find departmental classification users

DataTable Duser = EduOA.DBUtility.DbHelperSQL.Query ("select count (*) as count,d.Id as DId FROM OA_User ujie OA Department d where u.DepartmentID=d.Id group by d.Id") .Tables [0]

ExcelClass Ec = new ExcelClass (); / / create Excel operation class object

Int Ycount = 1

Ec.CreateExceFile (); / / create an Excel file

Ec.setValue (Ycount, 1, "CENTER", "organizational department")

Ec.setValue (Ycount, 2, "CENTER", "name")

Ec.setValue (Ycount, 3, "CENTER", "gender")

Ec.setValue (Ycount, 4, "CENTER", "position")

Ec.setValue (Ycount, 5, "CENTER", "Mobile phone")

Ec.setValue (Ycount, 6, "CENTER", "phone")

Ec.setValue (Ycount, 7, "CENTER", "email")

Ec.setBorder (1,1,1,1,50)

Ec.setBorder (1,2,2,2,20)

Ec.setBorder (1,5,5,5,20)

Ec.setBorder (1,6,6,6,20)

Ec.setBorder (1,7,7,7,20)

For (int I = 0; I < Duser.Rows.Count; iTunes +)

{

Ycount + = 1

Ec.setValue (Ycount, 1, "CENTER", Common.DeleteHtml (Getdept (Duser.Rows [I] ["count"], Duser.Rows [I] ["DId"])

DataTable dtuser = GetData (Duser.Rows [I] ["DId"])

For (int k = 0; k < dtuser.Rows.Count; knot +)

{

Ec.setValue (Ycount, 2, "CENTER", dtuser.Rows [k] ["TrueName"] .ToString ())

Ec.setValue (Ycount, 3, "CENTER", dtuser.Rows [k] ["sex"] .ToString ())

Ec.setValue (Ycount, 4, "CENTER", dtuser.Rows [k] ["PositionId"] .ToString ())

Ec.setValue (Ycount, 5, "CENTER", dtuser.Rows [k] ["Telephone"] .ToString ())

Ec.setValue (Ycount, 6, "CENTER", dtuser.Rows [k] ["Mobile"] .ToString ())

Ec.setValue (Ycount, 7, "CENTER", dtuser.Rows [k] ["Email"] .ToString ())

Ycount + = 1

}

}

String path = Server.MapPath ("Contactfiles\")

Ec.SaveAs (path+ address Book .xlsx)

/ / * release Excel resources *

Ec.ReleaseExcel ()

Response.Redirect ("Contactfiles/ address book .xlsx")

}

Catch (Exception ex)

{

PageError ("Export error!" + ex.ToString (), "")

}

}

At this point, I believe you have a deeper understanding of "how to write asp.net export Excel class library code". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report