界面內容如下
導出後顯示查詢到的數據如下
c#調用代碼如下
protected void btnOutput_Click(object sender
{
gvEquData
BindGridViewData()
ExcelHelper helper = new ExcelHelper()
helper
}
這裡我使用了NPOI這個dll來操作excel
public class ExcelHelper
{
#region NPOI Excel導出
/// <summary>
/// 導出Excel
/// </summary>
/// <param name=
/// <param name=
/// <param name=
/// <param name=
public void ExportExcel(GridView GV
{
HSSFWorkbook hssfworkbook = new HSSFWorkbook()
InitializeWorkbook(hssfworkbook
HSSFSheet sheet
HSSFPatriarch patriarch = (HSSFPatriarch)sheet
HSSFRow row;
HSSFCell cell;
//合並單元格信息
int startRow =
int startColumn =
int span =
int col =
//當前的格數
int rownum =
row = (HSSFRow)sheet
//添加Excel標題
for (int K =
{
cell = (HSSFCell)row
if (GV
{
ControlCollection cc=GV
if (cc
{
if (cc[
{
Literal ltl = cc[
cell
}
else
{
cell
}
}
else
{
if (cc[
{
Literal ltl = cc[
cell
}
else
{
cell
}
}
}
else
{
cell
}
//cell
}
//加載數據
for (int i =
{
row = (HSSFRow)sheet
rownum = i +
for (int j =
{
if (GV
{
cell = (HSSFCell)row
if (GV
{
CheckBox cbx = GV
if (cbx
{
cell
}
else
{
cell
}
}
}
else
{
TableCell Usecell = GV
if (Usecell
{
startRow = i +
startColumn = j;//起始列
span = Usecell
col = Usecell
}
cell = (HSSFCell)row
//當處於合並狀時忽略該格式內容
if (i +
{
}
else if (i +
{
//進行單元格的合並
int row
int col
sheet
cell
}
else
{
cell
}
}
}
}
//加載Footer部分數據
row = (HSSFRow)sheet
int footerAt =
int footSpan =
if (GV
{
for (int footNum =
{
TableCell footTc = GV
if (footTc
{
footSpan = footTc
footerAt = footNum;
}
cell = (HSSFCell)row
if (footNum > footerAt && footNum < footSpan + footerAt)
{
}
else if (footNum == footerAt)//合並單元格
{
int footercol
sheet
cell
}
else
{
cell
}
}
}
string path = ExcleName;
ExportToExcel(hssfworkbook
}
/// <summary>
/// 導出Excel
/// </summary>
/// <param name=
/// <param name=
/// <param name=
/// <param name=
/// <param name=
public void ExportExcel(DataTable Dt
{
HSSFWorkbook hssfworkbook = new HSSFWorkbook()
InitializeWorkbook(hssfworkbook
HSSFSheet excelSheet = (HSSFSheet)hssfworkbook
int rowCount =
HSSFRow newRow = (HSSFRow)excelSheet
rowCount++;
//循環寫出列頭
for (int i =
{
HSSFCell newCell = (HSSFCell)newRow
newCell
}
for (int i =
{
rowCount++;
HSSFRow newRowData = (HSSFRow)excelSheet
DataRow dr = Dt
for (int j =
{
HSSFCell newCell = (HSSFCell)newRow
newCell
}
}
string path = ExcleName;
ExportToExcel(hssfworkbook
}
//獲取圖片路徑
string getCellText(TableCell tc)
{
string result = HttpUtility
foreach (Control child in tc
{
if (child is Label)
{
result = HttpUtility
result = result
break;
}
}
string textLast = result
return textLast;
}
/// <summary>
/// 對產生的Excel進行文本輸入
/// </summary>
/// <param name=
public void WriteToFile(string Path)
{
////Write the stream data of workbook to the root directory
//FileStream file = new FileStream(Path
//hssfworkbook
//file
}
/// <summary>
/// 填寫Excel文本屬性 如有需要可以進行函數擴展 添加更多的屬性值
/// </summary>
/// <param name=
/// <param name=
public void InitializeWorkbook(HSSFWorkbook hssfworkbook
{
//hssfworkbook = new HSSFWorkbook()
//create a entry of DocumentSummaryInformation
DocumentSummaryInformation dsi = PropertySetFactory
dsi
hssfworkbook
//create a entry of SummaryInformation
SummaryInformation si = PropertySetFactory
si
hssfworkbook
}
MemoryStream WriteToStream(HSSFWorkbook hssfworkbook)
{
//Write the stream data of workbook to the root directory
MemoryStream file = new MemoryStream()
hssfworkbook
return file;
}
public void ExportToExcel(HSSFWorkbook hssfworkbook
{
#region //以字符流的形式下載文件
//FileStream fs = new FileStream(Apppath + filePath
//byte[] bytes = new byte[(int)fs
//fs
//fs
#endregion
HttpContext
HttpContext
HttpUtility
HttpContext
//HttpContext
HttpContext
HttpContext
//HttpContext
//HttpContext
}
#endregion
}
From:http://tw.wingwit.com/Article/program/net/201311/12029.html