//请求一个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. treegrid and datagrid ctrl or shift selectRow

    var tg = $('#tg');tg.treegrid({...}); // create treegridvar panel = tg.treegrid('getPanel');var body ...

  2. python 注意事项

    常见错误 #4:  不理解Python的作用域 Python是基于 LEGB 来进行作用于解析的, LEGB 是 Local, Enclosing, Global, Built-in 的缩写.看起来“ ...

  3. Oracle子查询(嵌套查询)

    概念: 所谓子查询,即一个select语句中嵌套了另外的一个或者多个select语句 需求:查找和Smith同部门的所有员工的id和last_name 目标: 员工id,last_name from: ...

  4. Java 中的 static 使用之静态初始化块

    Java 中可以通过初始化块进行数据赋值.如: 在类的声明中,可以包含多个初始化块,当创建类的实例时,就会依次执行这些代码块.如果使用 static 修饰初始化块,就称为静态初始化块. 需要特别注意: ...

  5. 任意阶幻方(魔方矩阵)C语言实现

    魔方又称幻方.纵横图.九宫图,最早记录于我国古代的洛书.据说夏禹治水时,河南洛阳附近的大河里浮出了一只乌龟,背上有一个很奇怪的图形,古人认为是一种祥瑞,预示着洪水将被夏禹王彻底制服.后人称之为&quo ...

  6. C++11带来的优雅语法

    C++11带来的优雅语法 自动类型推导 auto auto的自动类型推导,用于从初始化表达式中推断出变量的数据类型.通过auto的自动类型推导,可以简化我们的编程工作; auto是在编译时对变量进行了 ...

  7. 跨站脚本 XSS<一:防御方法>

    1. 过滤特殊字符 避免XSS的方法之一主要是将用户所提供的内容进行过滤,许多语言都有提供对HTML的过滤: PHP的htmlentities()或是htmlspecialchars(). Pytho ...

  8. Android OkHttp完全解析 是时候来了解OkHttp了

    Android OkHttp完全解析 是时候来了解OkHttp了 标签: AndroidOkHttp 2015-08-24 15:36 316254人阅读 评论(306) 收藏 举报  分类: [an ...

  9. JMS消息中间件系列[ActiveMQ](一)

    版本5.13.3的特性: 1.Supports a variety of Cross Language Clients and Protocols from Java, C, C++, C#, Rub ...

  10. __VA_ARGS__可变参数宏

    #define qWiFiDebug(format, ...) qDebug("[WiFi] "format" File:%s, Line:%d, Function:%s ...