asp.net fine ui 导出EXCEL

2024-08-23 03:18
文章标签 excel ui 导出 asp net fine

本文主要是介绍asp.net fine ui 导出EXCEL,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

A.基础

Microsoft.Office.Interop.Excel.Application app = null;
Microsoft.Office.Interop.Excel.Workbook workbook = null;
Microsoft.Office.Interop.Excel.Worksheet sheet = null;
object missing = System.Reflection.Missing.Value;
string strPath = string.Empty;
string strFileName = string.Empty;
try
{   app = new Microsoft.Office.Interop.Excel.Application();
app.Visible = false;        // 不显示,仅后台生成
workbook = app.Workbooks.Add(true); // 如果打开已存在文件,这里用Open,保存使用Save
// 添加sheet的方法
workbook.Worksheets.Add(missing, missing, missing, missing);
// 修改sheet名的方法,注意sheet的下标从1开始
sheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
sheet.Name = "统计";
// 设置单元格的wrap text属性  
sheet.Range[sheet.Cells[1, 1], sheet.Cells[intRowCount+4, 29]].WrapText = false;
sheet.Range[sheet.Cells[1, 1], sheet.Cells[intRowCount + 4, 29]].NumberFormat = "@";
sheet.Range[sheet.Cells[1, 1], sheet.Cells[intRowCount + 4, 29]].EntireRow.AutoFit();
sheet.Range[sheet.Cells[1, 1], sheet.Cells[intRowCount + 4, 29]].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
//sheet.Range[sheet.Cells[1, 9], sheet.Cells[30, 9]).NumberFormat = "YYYY-MM-DD HH:mm:ss";
//设置自动调整列宽
sheet.Range[sheet.Cells[1, 1], sheet.Cells[intRowCount + 4, 29]].EntireColumn.AutoFit();
sheet.Range[sheet.Cells[2, 1], sheet.Cells[intRowCount + 4, 29]].Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
sheet.Range[sheet.Cells[2, 1], sheet.Cells[intRowCount + 4, 29]].Borders.Weight = 2;
sheet.Range[sheet.Cells[3, 11], sheet.Cells[4, 11]].Merge(0);
sheet.Range[sheet.Cells[3, 11], sheet.Cells[4, 11]].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
sheet.Range[sheet.Cells[1, 1], sheet.Cells[intRowCount + 4, 29]].Value2 = arr;
// 这里给sheet填充内容部分略
// 如果文件已经存在又不想让“是否替换”的提示窗体显示出来,需要先调用File.Delete来删除文件
strFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx";
strPath = Server.MapPath("~/upload");
workbook.SaveAs(strPath + "\\" + strFileName, missing, missing, missing, missing, missing,
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, missing, missing,
missing, missing, missing);
Array arr = Array.CreateInstance(typeof(object), intRowCount + 4, 29);
arr.SetValue("区县", 1, 0);
int i = 1
foreach (DataRow row in myData.Tables[0].Rows)
{
arr.SetValue(row["DWNAME"], i, 0);
i++;
}
// 设置array数据,注意选择的行数和列数要与array行数和列数对应
sheet.Range[sheet.Cells[1, 1], sheet.Cells[intRowCount + 4, 29]].Value2 = arr;
//HyperLink1.NavigateUrl = string.Format(@"{0}/{1}/{2}", PageBase.UrlBase, PageBase.UploadDir, strFileName);
//HyperLink1.Text = string.Format("<span  style='color:red;font-weight:bold;'>点这里下载Excel文件{0}</span>", strFileName);
workbook.Close(false, missing, missing);
app.Quit();
workbook = null;
app = null;
GC.Collect();
FileInfo fi = new FileInfo(strPath + "\\" + strFileName);//excelFile为文件在服务器上的地址 
Response.ClearContent();
Response.AppendHeader("Content-Disposition", $"attachment;filename={strFileName}");
Response.ContentType = "application/excel";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.WriteFile(fi.FullName);
Response.Flush();
fi.Delete();
//Response.End();
}
catch (Exception ex)
{
if (app != null)
{
app.Quit();
app = null;
GC.Collect();
}
strError = ex.Message;
ShowNotify(strError, MessageBoxIcon.Error);
}
finally
{
if (app != null)
{
app.Quit();
workbook = null;
app = null;
GC.Collect();
}
}

B返回下载链接

#region 简易的Excel导出,没有单元格合并的功能。
/// <summary>
/// 简易的Excel导出,没有单元格合并的功能。
/// </summary>
/// <param name="myTable">要导出的数据表</param>
/// <param name="lstCol">要导出的列</param>
/// <returns>生成的excel文件名</returns>
public static string ExportExcel(DataTable myTable, List<string> lstCol)
{
Microsoft.Office.Interop.Excel.Application app = null;
Microsoft.Office.Interop.Excel.Workbook workbook = null;
Microsoft.Office.Interop.Excel.Worksheet sheet = null;
try
{
object missing = System.Reflection.Missing.Value;
app = new Microsoft.Office.Interop.Excel.Application();
app.Visible = false;        // 不显示,仅后台生成
workbook = app.Workbooks.Add(true); // 如果打开已存在文件,这里用Open,保存使用Save
// 添加sheet的方法
workbook.Worksheets.Add(missing, missing, missing, missing);
// 修改sheet名的方法,注意sheet的下标从1开始
sheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
sheet.Name = myTable.TableName;
if (myTable == null || myTable.Rows.Count == 0)
throw new Exception("传入的数据为空");
//行数
int intRowCount = myTable.Rows.Count;
//列数
int intColCount = lstCol.Count;
sheet.Range[sheet.Cells[1, 1], sheet.Cells[intRowCount + 1, intColCount]].WrapText = true;
// 设置单元格的数据格式
sheet.Range[sheet.Cells[1, 1], sheet.Cells[intRowCount + 1, intColCount]].NumberFormat = "@";
// 设置自动调整列宽,设置固定列宽
// sheet.get_Range(sheet.Cells[1, 1], sheet.Cells[5001, 5]).EntireColumn.AutoFit();
sheet.Range[sheet.Cells[1, 1], sheet.Cells[intRowCount + 1, intColCount]].EntireColumn.ColumnWidth = 20;
//设置单元格居中
//垂直居中
sheet.Range[sheet.Cells[1, 1], sheet.Cells[intRowCount + 1, intColCount]].VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;
//水平居中
sheet.Range[sheet.Cells[1, 1], sheet.Cells[intRowCount + 1, intColCount]].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
Array arr = Array.CreateInstance(typeof(object), intRowCount + 1, intColCount);
int intColIndex = 1;
foreach (string col in lstCol)
{
sheet.Range[sheet.Cells[1, intColIndex], sheet.Cells[1, intColIndex]].Value2 = col;
//sheet.Cells[1, intColIndex] = col;
intColIndex++;
}
// 不断的调用下面的的函数填充array中的内容
// 行,列均从0开始
for (int i = 0; i < intRowCount; i++)
{
for (int j = 0; j < intColCount; j++)
{
arr.SetValue(myTable.Rows[i][lstCol[j]], i, j);
}
}
// 设置array数据,注意选择的行数和列数要与array行数和列数对应
sheet.Range[sheet.Cells[2, 1], sheet.Cells[intRowCount + 1, intColCount]].Value2 = arr;
// 这里给sheet填充内容部分略
// 如果文件已经存在又不想让“是否替换”的提示窗体显示出来,需要先调用File.Delete来删除文件
string strFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx";
string strPath = HttpContext.Current.Server.MapPath(PageBase.UploadUrl);
workbook.SaveAs(strPath + "\\" + strFileName, missing, missing, missing, missing, missing,
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, missing, missing,
missing, missing, missing);
return strFileName;
}
catch (Exception exp)
{
throw new Exception(exp.Message);
}
finally
{
app.Quit();
Marshal.ReleaseComObject(sheet);
Marshal.ReleaseComObject(workbook);
Marshal.ReleaseComObject(app);
sheet = null;
workbook = null;
app = null;
GC.Collect();
}
}
#endregion

C浏览器流下载

#region 简易的Excel导出,没有单元格合并的功能。
/// <summary>
/// 简易的Excel导出,没有单元格合并的功能。
/// </summary>
/// <param name="myTable">要导出的数据表</param>
/// <param name="lstCol">要导出的列</param>
/// <returns>生成的excel文件名</returns>
public static void ExportExcel(DataTable myTable, List<string> lstCol, HttpResponse Response)
{
Microsoft.Office.Interop.Excel.Application app = null;
Microsoft.Office.Interop.Excel.Workbook workbook = null;
Microsoft.Office.Interop.Excel.Worksheet sheet = null;
try
{
object missing = System.Reflection.Missing.Value;
app = new Microsoft.Office.Interop.Excel.Application();
app.Visible = false;        // 不显示,仅后台生成
workbook = app.Workbooks.Add(true); // 如果打开已存在文件,这里用Open,保存使用Save
// 添加sheet的方法
workbook.Worksheets.Add(missing, missing, missing, missing);
// 修改sheet名的方法,注意sheet的下标从1开始
sheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
sheet.Name = myTable.TableName;
if (myTable == null || myTable.Rows.Count == 0)
throw new Exception("传入的数据为空");
int intRowCount = myTable.Rows.Count;
int intColCount = lstCol.Count;
sheet.Range[sheet.Cells[1, 1], sheet.Cells[intRowCount + 1, intColCount]].WrapText = true;
// 设置单元格的数据格式
sheet.Range[sheet.Cells[1, 1], sheet.Cells[intRowCount + 1, intColCount]].NumberFormat = "@";
// 设置自动调整列宽,设置固定列宽
// sheet.get_Range(sheet.Cells[1, 1], sheet.Cells[5001, 5]).EntireColumn.AutoFit();
sheet.Range[sheet.Cells[1, 1], sheet.Cells[intRowCount + 1, intColCount]].EntireColumn.ColumnWidth = 20;
//设置单元格居中
//垂直居中
sheet.Range[sheet.Cells[1, 1], sheet.Cells[intRowCount + 1, intColCount]].VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;
//水平居中
sheet.Range[sheet.Cells[1, 1], sheet.Cells[intRowCount + 1, intColCount]].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
Array arr = Array.CreateInstance(typeof(object), intRowCount + 1, intColCount);
int intColIndex = 1;
foreach (string col in lstCol)
{
sheet.Range[sheet.Cells[1, intColIndex], sheet.Cells[1, intColIndex]].Value2 = col;
//sheet.Cells[1, intColIndex] = col;
intColIndex++;
}
// 不断的调用下面的的函数填充array中的内容
// 行,列均从0开始
for (int i = 0; i < intRowCount; i++)
{
for (int j = 0; j < intColCount; j++)
{
arr.SetValue(myTable.Rows[i][lstCol[j]], i, j);
}
}
// 设置array数据,注意选择的行数和列数要与array行数和列数对应
sheet.Range[sheet.Cells[2, 1], sheet.Cells[intRowCount + 1, intColCount]].Value2 = arr;
// 这里给sheet填充内容部分略
// 如果文件已经存在又不想让“是否替换”的提示窗体显示出来,需要先调用File.Delete来删除文件
string strFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx";
string strPath = HttpContext.Current.Server.MapPath(PageBase.UploadUrl);
workbook.SaveAs(strPath + "\\" + strFileName, missing, missing, missing, missing, missing,
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, missing, missing,
missing, missing, missing);
app.Quit();
Marshal.ReleaseComObject(sheet);
Marshal.ReleaseComObject(workbook);
Marshal.ReleaseComObject(app);
sheet = null;
workbook = null;
app = null;
GC.Collect();
using (FileStream fs = new FileStream(strPath + "\\" + strFileName, FileMode.Open))
{
byte[] bFile = new byte[fs.Length];
fs.Read(bFile, 0, bFile.Length);
fs.Close();
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8));
Response.BinaryWrite(bFile);
Response.Flush();
Response.Close();
}
}
catch (Exception exp)
{
throw new Exception(exp.Message);
}
finally
{
if (app != null)
{
app.Quit();
Marshal.ReleaseComObject(sheet);
Marshal.ReleaseComObject(workbook);
Marshal.ReleaseComObject(app);
sheet = null;
workbook = null;
app = null;
GC.Collect();
}
}
}

这篇关于asp.net fine ui 导出EXCEL的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1098200

相关文章

使用Java将各种数据写入Excel表格的操作示例

《使用Java将各种数据写入Excel表格的操作示例》在数据处理与管理领域,Excel凭借其强大的功能和广泛的应用,成为了数据存储与展示的重要工具,在Java开发过程中,常常需要将不同类型的数据,本文... 目录前言安装免费Java库1. 写入文本、或数值到 Excel单元格2. 写入数组到 Excel表格

利用Python打造一个Excel记账模板

《利用Python打造一个Excel记账模板》这篇文章主要为大家详细介绍了如何使用Python打造一个超实用的Excel记账模板,可以帮助大家高效管理财务,迈向财富自由之路,感兴趣的小伙伴快跟随小编一... 目录设置预算百分比超支标红预警记账模板功能介绍基础记账预算管理可视化分析摸鱼时间理财法碎片时间利用财

利用python实现对excel文件进行加密

《利用python实现对excel文件进行加密》由于文件内容的私密性,需要对Excel文件进行加密,保护文件以免给第三方看到,本文将以Python语言为例,和大家讲讲如何对Excel文件进行加密,感兴... 目录前言方法一:使用pywin32库(仅限Windows)方法二:使用msoffcrypto-too

如何使用 Python 读取 Excel 数据

《如何使用Python读取Excel数据》:本文主要介绍使用Python读取Excel数据的详细教程,通过pandas和openpyxl,你可以轻松读取Excel文件,并进行各种数据处理操... 目录使用 python 读取 Excel 数据的详细教程1. 安装必要的依赖2. 读取 Excel 文件3. 读

C#实现将Excel表格转换为图片(JPG/ PNG)

《C#实现将Excel表格转换为图片(JPG/PNG)》Excel表格可能会因为不同设备或字体缺失等问题,导致格式错乱或数据显示异常,转换为图片后,能确保数据的排版等保持一致,下面我们看看如何使用C... 目录通过C# 转换Excel工作表到图片通过C# 转换指定单元格区域到图片知识扩展C# 将 Excel

使用Python将JSON,XML和YAML数据写入Excel文件

《使用Python将JSON,XML和YAML数据写入Excel文件》JSON、XML和YAML作为主流结构化数据格式,因其层次化表达能力和跨平台兼容性,已成为系统间数据交换的通用载体,本文将介绍如何... 目录如何使用python写入数据到Excel工作表用Python导入jsON数据到Excel工作表用

Python将博客内容html导出为Markdown格式

《Python将博客内容html导出为Markdown格式》Python将博客内容html导出为Markdown格式,通过博客url地址抓取文章,分析并提取出文章标题和内容,将内容构建成html,再转... 目录一、为什么要搞?二、准备如何搞?三、说搞咱就搞!抓取文章提取内容构建html转存markdown

vue使用docxtemplater导出word

《vue使用docxtemplater导出word》docxtemplater是一种邮件合并工具,以编程方式使用并处理条件、循环,并且可以扩展以插入任何内容,下面我们来看看如何使用docxtempl... 目录docxtemplatervue使用docxtemplater导出word安装常用语法 封装导出方

java中使用POI生成Excel并导出过程

《java中使用POI生成Excel并导出过程》:本文主要介绍java中使用POI生成Excel并导出过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录需求说明及实现方式需求完成通用代码版本1版本2结果展示type参数为atype参数为b总结注:本文章中代码均为

利用Python开发Markdown表格结构转换为Excel工具

《利用Python开发Markdown表格结构转换为Excel工具》在数据管理和文档编写过程中,我们经常使用Markdown来记录表格数据,但它没有Excel使用方便,所以本文将使用Python编写一... 目录1.完整代码2. 项目概述3. 代码解析3.1 依赖库3.2 GUI 设计3.3 解析 Mark