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

相关文章

springboot集成easypoi导出word换行处理过程

《springboot集成easypoi导出word换行处理过程》SpringBoot集成Easypoi导出Word时,换行符n失效显示为空格,解决方法包括生成段落或替换模板中n为回车,同时需确... 目录项目场景问题描述解决方案第一种:生成段落的方式第二种:替换模板的情况,换行符替换成回车总结项目场景s

Python实现Excel批量样式修改器(附完整代码)

《Python实现Excel批量样式修改器(附完整代码)》这篇文章主要为大家详细介绍了如何使用Python实现一个Excel批量样式修改器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录前言功能特性核心功能界面特性系统要求安装说明使用指南基本操作流程高级功能技术实现核心技术栈关键函

oracle 11g导入\导出(expdp impdp)之导入过程

《oracle11g导入导出(expdpimpdp)之导入过程》导出需使用SEC.DMP格式,无分号;建立expdir目录(E:/exp)并确保存在;导入在cmd下执行,需sys用户权限;若需修... 目录准备文件导入(impdp)1、建立directory2、导入语句 3、更改密码总结上一个环节,我们讲了

Python实现批量CSV转Excel的高性能处理方案

《Python实现批量CSV转Excel的高性能处理方案》在日常办公中,我们经常需要将CSV格式的数据转换为Excel文件,本文将介绍一个基于Python的高性能解决方案,感兴趣的小伙伴可以跟随小编一... 目录一、场景需求二、技术方案三、核心代码四、批量处理方案五、性能优化六、使用示例完整代码七、小结一、

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1

C#使用Spire.XLS快速生成多表格Excel文件

《C#使用Spire.XLS快速生成多表格Excel文件》在日常开发中,我们经常需要将业务数据导出为结构清晰的Excel文件,本文将手把手教你使用Spire.XLS这个强大的.NET组件,只需几行C#... 目录一、Spire.XLS核心优势清单1.1 性能碾压:从3秒到0.5秒的质变1.2 批量操作的优雅

Go语言使用net/http构建一个RESTful API的示例代码

《Go语言使用net/http构建一个RESTfulAPI的示例代码》Go的标准库net/http提供了构建Web服务所需的强大功能,虽然众多第三方框架(如Gin、Echo)已经封装了很多功能,但... 目录引言一、什么是 RESTful API?二、实战目标:用户信息管理 API三、代码实现1. 用户数据

在ASP.NET项目中如何使用C#生成二维码

《在ASP.NET项目中如何使用C#生成二维码》二维码(QRCode)已广泛应用于网址分享,支付链接等场景,本文将以ASP.NET为示例,演示如何实现输入文本/URL,生成二维码,在线显示与下载的完整... 目录创建前端页面(Index.cshtml)后端二维码生成逻辑(Index.cshtml.cs)总结

Android 缓存日志Logcat导出与分析最佳实践

《Android缓存日志Logcat导出与分析最佳实践》本文全面介绍AndroidLogcat缓存日志的导出与分析方法,涵盖按进程、缓冲区类型及日志级别过滤,自动化工具使用,常见问题解决方案和最佳实... 目录android 缓存日志(Logcat)导出与分析全攻略为什么要导出缓存日志?按需过滤导出1. 按

Qt中实现多线程导出数据功能的四种方式小结

《Qt中实现多线程导出数据功能的四种方式小结》在以往的项目开发中,在很多地方用到了多线程,本文将记录下在Qt开发中用到的多线程技术实现方法,以导出指定范围的数字到txt文件为例,展示多线程不同的实现方... 目录前言导出文件的示例工具类QThreadQObject的moveToThread方法实现多线程QC