//请求一个excel类
Microsoft.Office.Interop.Excel.ApplicationClass excel = null;
//创建 Workbook对象
Microsoft.Office.Interop.Excel._Workbook workbook = null;
Microsoft.Office.Interop.Excel.Workbooks workbooks = null;
//创建工作薄
Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
try
{
excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
//要保存的文件名
// string fulllFileName = fileName;
object missing = System.Reflection.Missing.Value;
try
{
workbooks = excel.Workbooks;
workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
Microsoft.Office.Interop.Excel.Range range;

// long totalCount = data.Count + 2;

worksheet.Columns.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignRight;//设置水平对齐方式 为居右
range = excel.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, 5]);
range.Font.Bold = "true";
range.Font.Size = "16";
range.Merge(false);//合并单元格
range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;

//range.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, System.Drawing.Color.Black.ToArgb());
worksheet.Cells[1, 1] = "value";//设置excel的大标题

//range.Interior.ColorIndex = "15";
//range.Font.Bold = "true";
int r = 1;
int c = 0;

我是按照一行五个单元格排列的,并且根据需要凡是包括\\r\\n字符串的字符串均作为标题,设置其样式(颜色,字体大小,并合并该行的单元格),此后一格一个数据,页面内容如时间:2013年3月3日,那么excel里就存成

for (int i = 0; i < titleList.Count; i++)
{
string title = titleList[i];
if (title.Contains("\\r\\n"))
{
r = r + 2;
c = 0;
worksheet.Cells[r, c + 1] = title.Replace("\\r\\n", "");
range = excel.get_Range(worksheet.Cells[r, c + 1], worksheet.Cells[r, c + 5]);
range.Font.Bold = "true";
range.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);
range.Cells.EntireColumn.AutoFit();
//设置表格边框
range.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black));
//range.Font.Size = "16";
range.Merge(false);//合并单元格
range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;
range.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(36, 40, 50));
r = r + 1;
}
else
{
if (c <= 4)
{
range = excel.get_Range(worksheet.Cells[r, c + 1], worksheet.Cells[r, c + 1]);
range.Font.Bold = "true";
//设置表格边框
worksheet.Cells[r, c + 1] = title;
}
else
{
r = r + 2;
c = 0;
worksheet.Cells[r, c + 1] = title;
}
for (int n = 0; n < 5; n++)
{
range = excel.get_Range(worksheet.Cells[r, n + 1], worksheet.Cells[r, n + 1]);
range.Cells.EntireColumn.AutoFit();
range.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black));
}
c++;
}
}
c = 0;
r = 2;
for (int i = 0; i < contentList.Count; i++)
{
string content = contentList[i];
if (content.Contains("\\r\\n"))
{
r = r + 3;
c = 0;
}
else
{
if (c <= 4)
{
range = excel.get_Range(worksheet.Cells[r, c + 1], worksheet.Cells[r, c + 1]);
//设置表格边框
// range.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black));
worksheet.Cells[r, c + 1] = content;
}
else
{
r = r + 2;
c = 0;
worksheet.Cells[r, c + 1] = content;
}
for (int n = 0; n < 5; n++)
{
range = excel.get_Range(worksheet.Cells[r, n + 1], worksheet.Cells[r, n + 1]);
range.Cells.EntireColumn.AutoFit();
range.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black));
}
c++;
}
}

worksheet.Application.DisplayAlerts = false;//显示提示信息
worksheet.Name = fileName;

workbook.Saved=true;

workbook.SaveAs(Server.MapPath("/") + fileName + ".xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel7, missing, missing, missing, missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing, missing);//这里解决打开文件时提示格式与扩展名指定的格式不一致的问题,这里我用的是xls,所有XlFormat.xlExcel7.

//excel.SaveWorkspace(fileName);//这里保存文件的工作空间,在asp.net中就是实现了保存对话框打开的效果,只是我现在还没有找到设置默认文件名的办法,如果有知道的,麻烦赐教一下,谢谢啦
//excel.Save(fileName);
// Response.Redirect(@"c:\下载文件.xls");
}
catch (Exception ex)
{ }
finally
{
if (workbook != null)
{
workbook.Close(false, null, null);
workbook = null;
worksheet = null;
}
}
}
catch (Exception ex)
{ }
finally
{
if (excel != null)
{
excel.Quit();
excel = null;
//DoExcel();

//下载文件
FileInfo fileInfo = new FileInfo(Server.MapPath("/") + fileName + ".xls");
if (fileInfo.Exists)
{
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileInfo.Name, System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length", fileInfo.Length.ToString());
Response.WriteFile(fileInfo.FullName);
Response.Flush();
//Response.End();
}
else
{
//文件不存在
}

}
}

asp.net将页面内容按需导入Excel,并设置excel样式,下载文件(解决打开格式与扩展名指定的格式不统一的问题)的更多相关文章

  1. asp.net mvc 页面内容呈现Html.Raw HtmlString

    asp.net mvc 页面内容呈现Html.Raw Html.Raw内容经过页面呈现,不呈现Html标签 @Html.Raw( File.ReadAllText(Server.MapPath(&qu ...

  2. iis添加asp.net网站,访问提示:由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射

    今天在iis服务器配置asp.net网站,遇到一个问题,记录一下: 问题:由于扩展配置问题而无法提供您请求的页面.如果该页面是脚本,请添加处理程序.如果应下载文件,请添加 MIME 映射. Windo ...

  3. IIS 错误:由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射。

    HTTP 错误 404.3 - Not Found 由于扩展配置问题而无法提供您请求的页面.如果该页面是脚本,请添加处理程序.如果应下载文件,请添加 MIME 映射. 可能是缺少处理程序映射.默认情况 ...

  4. 解决问题:由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射。

    WindowServer2012服务器,添加角色安装完.netframework和iis之后,运行aspx页面就报如下错误: HTTP 错误 404.3 - Not Found 由于扩展配置问题而无法 ...

  5. 访问 .obj文件,由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射

    一.错误描述 HTTP 错误 404.3 - Not Found 由于扩展配置问题而无法提供您请求的页面.如果该页面是脚本,请添加处理程序.如果应下载文件,请添加 MIME 映射. 最可能的原因: 可 ...

  6. IIS虚拟目录内的视频文件访问出错:HTTP 错误 404.3 - Not Found 由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射。

    MIME类型就是设定某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访问的时候,浏览器会自动使用指定应用程序来打开.多用于指定一些客户端自定义的文件名,以及一些媒体文件打开方式. 我是 ...

  7. HTTP 错误 404.3 – Not Found 由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射。

    今天,在vs2013中新建了一个placard.json文件,当我用jq读取它的时候,去提示404,直接在浏览器访问这个文件,提示: HTTP 错误 404.3 – Not Found 由于扩展配置问 ...

  8. 关于asp.net C# 导出Excel文件 打开Excel文件格式与扩展名指定格式不一致的解决办法

    -----转载:http://blog.csdn.net/sgear/article/details/7663502 关于asp.net C# 导出Excel文件 打开Excel文件格式与扩展名指定格 ...

  9. asp.net 输出 页面内容 在服务器上

    .定义页面内容 按 Ctrl+C 复制代码 <asp:Content ID="BodyContent" runat="server" ContentPla ...

随机推荐

  1. R语言基础:数组&列表&向量&矩阵&因子&数据框

    R语言基础:数组和列表 数组(array) 一维数据是向量,二维数据是矩阵,数组是向量和矩阵的直接推广,是由三维或三维以上的数据构成的. 数组函数是array(),语法是:array(dadta, d ...

  2. [问题2015S07] 复旦高等代数 II(14级)每周一题(第八教学周)

    [问题2015S07]  设 \(A\) 为 \(n\) 阶复方阵, 证明: 存在 \(n\) 阶非异复对称阵 \(S\), 使得 \(A'=S^{-1}AS\), 即 \(A\) 可通过非异复对称阵 ...

  3. [问题2014S13] 复旦高等代数II(13级)每周一题(第十三教学周)

    [问题2014S13]  (1)  设 \(A\) 是数域 \(\mathbb{K}\) 上的 \(n\) 阶非异阵, 若存在主对角元全为 \(1\) 的下三角阵 \(L\in M_n(\mathbb ...

  4. Linux 休眠,挂起(待机),关机等几个命令的区别及如何实现;如何启用Ubuntu的休眠模式

    这里对linux 的几个命令整理下,有:休眠,挂起,待机,关机等几个命令的区别及如何实现. 休眠是一种更加省电的模式,它将内存中的数据保存于硬盘中,所有设备都停止工作.当再次使用时需按开关机键,机器将 ...

  5. [渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:为ASP.NET MVC应用程序处理并发

    这是微软官方教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译,这里是第十篇:为ASP.NET MVC应用程序 ...

  6. Windows下安装postgresql_psycopg2时出现 "Unabled to find vcvarsall.bat" 的解决办法

    使用django时会用到postgresql的数据库,如下表: 数据库引擎设置 设置 数据库 适配器 postgresql PostgreSQL psycopg 版本 1.x, http://www. ...

  7. html,css,js加载顺序

    1.js放在head中会立即执行,阻塞后续的资源下载与执行.因为js有可能会修改dom,如果不阻塞后续的资源下载,dom的操作顺序不可控. 正常的网页加载流程是这样的. 浏览器一边下载HTML网页,一 ...

  8. Python_Day2_基础2

    python基础之数据类型与变量 一.变量 变量作用:保存状态(程序的运行本质是一系列状态的变化,变量的目的就是用来保存状态,变量值的变化就构成了程序运行的不同结果.) Age=10 ----> ...

  9. AlwaysOn数据同步问题探究

    随着AlwaysOn技术的流行,关于AlwayOn的问题也越来越多,某企业搭建有三副本的AlwaysOn一套,现想修改主节点上某张表的某个数据,看看会出现什么后果,如果结果正常,就同步到其他节点上:如 ...

  10. Android SharePreference 在主进程和次进程间共享数据不同步出错

      SharedPreference作为android五大存储(网络,数据库,文件,SharedPreference,contentProvider)之中最方便使用的一个,从类名上来看就不是一个存储大 ...