本文主要是介绍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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!