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

相关文章

C#实现将Office文档(Word/Excel/PDF/PPT)转为Markdown格式

《C#实现将Office文档(Word/Excel/PDF/PPT)转为Markdown格式》Markdown凭借简洁的语法、优良的可读性,以及对版本控制系统的高度兼容性,逐渐成为最受欢迎的文档格式... 目录为什么要将文档转换为 Markdown 格式使用工具将 Word 文档转换为 Markdown(.

VS配置好Qt环境之后但无法打开ui界面的问题解决

《VS配置好Qt环境之后但无法打开ui界面的问题解决》本文主要介绍了VS配置好Qt环境之后但无法打开ui界面的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目UKeLvb录找到Qt安装目录中designer.UKeLvBexe的路径找到vs中的解决方案资源

使用C#删除Excel表格中的重复行数据的代码详解

《使用C#删除Excel表格中的重复行数据的代码详解》重复行是指在Excel表格中完全相同的多行数据,删除这些重复行至关重要,因为它们不仅会干扰数据分析,还可能导致错误的决策和结论,所以本文给大家介绍... 目录简介使用工具C# 删除Excel工作表中的重复行语法工作原理实现代码C# 删除指定Excel单元

Python实现pdf电子发票信息提取到excel表格

《Python实现pdf电子发票信息提取到excel表格》这篇文章主要为大家详细介绍了如何使用Python实现pdf电子发票信息提取并保存到excel表格,文中的示例代码讲解详细,感兴趣的小伙伴可以跟... 目录应用场景详细代码步骤总结优化应用场景电子发票信息提取系统主要应用于以下场景:企业财务部门:需

Mac备忘录怎么导出/备份和云同步? Mac备忘录使用技巧

《Mac备忘录怎么导出/备份和云同步?Mac备忘录使用技巧》备忘录作为iOS里简单而又不可或缺的一个系统应用,上手容易,可以满足我们日常生活中各种记录的需求,今天我们就来看看Mac备忘录的导出、... 「备忘录」是 MAC 上的一款常用应用,它可以帮助我们捕捉灵感、记录待办事项或保存重要信息。为了便于在不同

Python处理大量Excel文件的十个技巧分享

《Python处理大量Excel文件的十个技巧分享》每天被大量Excel文件折磨的你看过来!这是一份Python程序员整理的实用技巧,不说废话,直接上干货,文章通过代码示例讲解的非常详细,需要的朋友可... 目录一、批量读取多个Excel文件二、选择性读取工作表和列三、自动调整格式和样式四、智能数据清洗五、

Python Pandas高效处理Excel数据完整指南

《PythonPandas高效处理Excel数据完整指南》在数据驱动的时代,Excel仍是大量企业存储核心数据的工具,Python的Pandas库凭借其向量化计算、内存优化和丰富的数据处理接口,成为... 目录一、环境搭建与数据读取1.1 基础环境配置1.2 数据高效载入技巧二、数据清洗核心战术2.1 缺失

利用Python实现Excel文件智能合并工具

《利用Python实现Excel文件智能合并工具》有时候,我们需要将多个Excel文件按照特定顺序合并成一个文件,这样可以更方便地进行后续的数据处理和分析,下面我们看看如何使用Python实现Exce... 目录运行结果为什么需要这个工具技术实现工具的核心功能代码解析使用示例工具优化与扩展有时候,我们需要将

MySQL Workbench工具导出导入数据库方式

《MySQLWorkbench工具导出导入数据库方式》:本文主要介绍MySQLWorkbench工具导出导入数据库方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝... 目录mysql Workbench工具导出导入数据库第一步 www.chinasem.cn数据库导出第二步

Java如何根据word模板导出数据

《Java如何根据word模板导出数据》这篇文章主要为大家详细介绍了Java如何实现根据word模板导出数据,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... pom.XML文件导入依赖 <dependency> <groupId>cn.afterturn</groupId>