Response.ContentType 收集

'ez' => 'application/andrew-inset',
'hqx' => 'application/mac-binhex40',
'cpt' => 'application/mac-compactpro',
'doc' => 'application/msword',
'bin' => 'application/octet-stream',
'dms' => 'application/octet-stream',
'lha' => 'application/octet-stream',
'lzh' => 'application/octet-stream',
'exe' => 'application/octet-stream',
'class' => 'application/octet-stream',
'so' => 'application/octet-stream',
'dll' => 'application/octet-stream',
'oda' => 'application/oda',
'pdf' => 'application/pdf',
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'ps' => 'application/postscript',
'smi' => 'application/smil',
'smil' => 'application/smil',
'mif' => 'application/vnd.mif',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
'wbxml' => 'application/vnd.wap.wbxml',
'wmlc' => 'application/vnd.wap.wmlc',
'wmlsc' => 'application/vnd.wap.wmlscriptc',
'bcpio' => 'application/x-bcpio',
'vcd' => 'application/x-cdlink',
'pgn' => 'application/x-chess-pgn',
'cpio' => 'application/x-cpio',
'csh' => 'application/x-csh',
'dcr' => 'application/x-director',
'dir' => 'application/x-director',
'dxr' => 'application/x-director',
'dvi' => 'application/x-dvi',
'spl' => 'application/x-futuresplash',
'gtar' => 'application/x-gtar',
'hdf' => 'application/x-hdf',
'js' => 'application/x-javascript',
'skp' => 'application/x-koan',
'skd' => 'application/x-koan',
'skt' => 'application/x-koan',
'skm' => 'application/x-koan',
'latex' => 'application/x-latex',
'nc' => 'application/x-netcdf',
'cdf' => 'application/x-netcdf',
'sh' => 'application/x-sh',
'shar' => 'application/x-shar',
'swf' => 'application/x-shockwave-flash',
'sit' => 'application/x-stuffit',
'sv4cpio' => 'application/x-sv4cpio',
'sv4crc' => 'application/x-sv4crc',
'tar' => 'application/x-tar',
'tcl' => 'application/x-tcl',
'tex' => 'application/x-tex',
'texinfo' => 'application/x-texinfo',
'texi' => 'application/x-texinfo',
't' => 'application/x-troff',
'tr' => 'application/x-troff',
'roff' => 'application/x-troff',
'man' => 'application/x-troff-man',
'me' => 'application/x-troff-me',
'ms' => 'application/x-troff-ms',
'ustar' => 'application/x-ustar',
'src' => 'application/x-wais-source',
'xhtml' => 'application/xhtml+xml',
'xht' => 'application/xhtml+xml',
'zip' => 'application/zip',
'au' => 'audio/basic',
'snd' => 'audio/basic',
'mid' => 'audio/midi',
'midi' => 'audio/midi',
'kar' => 'audio/midi',
'mpga' => 'audio/mpeg',
'mp2' => 'audio/mpeg',
'mp3' => 'audio/mpeg',
'aif' => 'audio/x-aiff',
'aiff' => 'audio/x-aiff',
'aifc' => 'audio/x-aiff',
'm3u' => 'audio/x-mpegurl',
'ram' => 'audio/x-pn-realaudio',
'rm' => 'audio/x-pn-realaudio',
'rpm' => 'audio/x-pn-realaudio-plugin',
'ra' => 'audio/x-realaudio',
'wav' => 'audio/x-wav',
'pdb' => 'chemical/x-pdb',
'xyz' => 'chemical/x-xyz',
'bmp' => 'image/bmp',
'gif' => 'image/gif',
'ief' => 'image/ief',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'jpe' => 'image/jpeg',
'png' => 'image/png',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'djvu' => 'image/vnd.djvu',
'djv' => 'image/vnd.djvu',
'wbmp' => 'image/vnd.wap.wbmp',
'ras' => 'image/x-cmu-raster',
'pnm' => 'image/x-portable-anymap',
'pbm' => 'image/x-portable-bitmap',
'pgm' => 'image/x-portable-graymap',
'ppm' => 'image/x-portable-pixmap',
'rgb' => 'image/x-rgb',
'xbm' => 'image/x-xbitmap',
'xpm' => 'image/x-xpixmap',
'xwd' => 'image/x-xwindowdump',
'igs' => 'model/iges',
'iges' => 'model/iges',
'msh' => 'model/mesh',
'mesh' => 'model/mesh',
'silo' => 'model/mesh',
'wrl' => 'model/vrml',
'vrml' => 'model/vrml',
'css' => 'text/css',
'html' => 'text/html',
'htm' => 'text/html',
'asc' => 'text/plain',
'txt' => 'text/plain',
'rtx' => 'text/richtext',
'rtf' => 'text/rtf',
'sgml' => 'text/sgml',
'sgm' => 'text/sgml',
'tsv' => 'text/tab-separated-values',
'wml' => 'text/vnd.wap.wml',
'wmls' => 'text/vnd.wap.wmlscript',
'etx' => 'text/x-setext',
'xsl' => 'text/xml',
'xml' => 'text/xml',
'mpeg' => 'video/mpeg',
'mpg' => 'video/mpeg',
'mpe' => 'video/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
'mxu' => 'video/vnd.mpegurl',
'avi' => 'video/x-msvideo',
'movie' => 'video/x-sgi-movie',
'ice' => 'x-conference/x-cooltalk'

Office2007对应的值:

"application/vnd.openxmlformats-officedocument.wordprocessingml.template" (for .dotx files)

"application/vnd.openxmlformats-officedocument.presentationml.presentation" (for .pptx files)

"application/vnd.openxmlformats-officedocument.presentationml.slideshow" (for .ppsx files)

"application/vnd.openxmlformats-officedocument.presentationml.template" (for .potx files)

"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" (for .xlsx files)

"application/vnd.openxmlformats-officedocument.spreadsheetml.template" (for .xltx files)

相对于Office2003是这样的

Response.ContentType = "application/vnd。ms-excel"


作者:Meteor
出处:http://www.cnblogs.com/osmeteor/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

如果您看了本篇博客,觉得对您有所收获,请点击右下角的 [推荐]

帮我在以下代码里加一个处理json字符串特殊字符的功能using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Web; using System.Web.UI; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using OfficeOpenXml; namespace JsonToExcelWeb { public partial class UploadPage : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnUpload_Click(object sender, EventArgs e) { if (FileUpload1.HasFile) { try { // 获取上传的文件 HttpPostedFile file = FileUpload1.PostedFile; // 读取文件内容 using (StreamReader reader = new StreamReader(file.InputStream)) { string jsonContent = reader.ReadToEnd(); // 将JSON转换为DataTable DataTable dt = JsonToDataTable(jsonContent); // 生成Excel并返回给用户 GenerateExcelFile(dt, "converted.xlsx"); } } catch (Exception ex) { lblMessage.Text = "Error: " + ex.Message; } } else { lblMessage.Text = "Please select a file."; } } private DataTable JsonToDataTable(string jsonString) { // 解析JSON字符串 JArray jsonArray = JArray.Parse(jsonString); DataTable dt = new DataTable(); // 如果数组为空,则返回空表 if (jsonArray.Count == 0) return dt; // 收集列名(考虑嵌套对象,这里我们简单处理:只取第一行元素的所有属性,且如果属性是对象或数组,则将其转换为字符串) JObject firstRow = (JObject)jsonArray[0]; foreach (JProperty property in firstRow.Properties()) { // 根据值的类型来创建列 JToken value = firstRow[property.Name]; Type columnType = GetColumnType(value); dt.Columns.Add(property.Name, columnType); } // 遍历每一行 foreach (JObject row in jsonArray) { DataRow dr = dt.NewRow(); foreach (DataColumn column in dt.Columns) { JToken token = row[column.ColumnName]; if (token != null) { // 如果类型是复杂类型,则序列化为字符串;否则直接取值 if (token.Type == JTokenType.Object || token.Type == JTokenType.Array) { dr[column] = token.ToString(Formatting.None); } else { dr[column] = token.ToObject(column.DataType); } } } dt.Rows.Add(dr); } return dt; } private Type GetColumnType(JToken token) { if (token == null) return typeof(string); switch (token.Type) { case JTokenType.Integer: return typeof(long); case JTokenType.Float: return typeof(double); case JTokenType.Boolean: return typeof(bool); case JTokenType.Date: return typeof(DateTime); case JTokenType.Object: case JTokenType.Array: return typeof(string); // 复杂类型存储为字符串 default: return typeof(string); } } private void GenerateExcelFile(DataTable dt, string fileName) { // 使用EPPlus using (ExcelPackage package = new ExcelPackage()) { ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Sheet1"); // 加载数据表 worksheet.Cells["A1"].LoadFromDataTable(dt, true); // 设置响应头 Response.Clear(); Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; Response.AddHeader("content-disposition", "attachment; filename=" + fileName); // 将Excel写入响应流 Response.BinaryWrite(package.GetAsByteArray()); Response.Flush(); Response.End(); } } } }
最新发布
08-13
package com.corilead.utlis; import com.alibaba.fastjson.JSON; import com.casic.cplm.audit.dto.AuditTrailDto; import java.time.Instant; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; import org.springframework.web.reactive.function.client.*; import reactor.core.publisher.Mono; public class ParamFilter implements ExchangeFilterFunction { public static final String CUSTOM_PARAM_KEY = "customParam"; private static final Logger log = LoggerFactory.getLogger(ParamFilter.class); @Override public Mono<ClientResponse> filter(ClientRequest request, ExchangeFunction next) { // 创建操作日志 AuditTrailDto operLog = new AuditTrailDto(); String requestBody = (String) request.attributes().getOrDefault(CUSTOM_PARAM_KEY, ""); // 设置操作日志基本信息 setupBaseOperationInfo(operLog, request, requestBody); return next.exchange(request) .timeout(java.time.Duration.ofSeconds(5)) .flatMap( response -> response .bodyToMono(byte[].class) .defaultIfEmpty(new byte[0]) .flatMap( bodyBytes -> { String bodyStr = formatResponseBody(bodyBytes); updateOperLogWithResponse(operLog, response, bodyStr); return buildClientResponse(response, bodyStr); })) .onErrorResume(e -> handleError(e, operLog)); } /** 格式化响应体内容 */ private String formatResponseBody(byte[] bodyBytes) { return bodyBytes.length > 0 ? new String(bodyBytes, java.nio.charset.StandardCharsets.UTF_8) : "[empty-body]"; } /** 更新操作日志的响应相关信息 */ private void updateOperLogWithResponse( AuditTrailDto operLog, ClientResponse response, String bodyStr) { operLog.setDescription(JSON.toJSONString(bodyStr)); operLog.setStatus(response.statusCode().value()); // 保存数据库 // auditTrailService.createAuditTrail(operLog); log.info("系统内部调用第三方接口: " + operLog); } /** 构建新的客户端响应 */ private Mono<ClientResponse> buildClientResponse(ClientResponse response, String bodyStr) { return Mono.just(ClientResponse.from(response).body(bodyStr).build()); } /** 设置操作日志的基本信息 */ private void setupBaseOperationInfo( AuditTrailDto operLog, ClientRequest request, String requestBody) { operLog.setEventName("系统内部调用第三方"); operLog.setExtraParams(requestBody); operLog.setResourceType(request.method().name()); operLog.setRequestPath(request.url().getPath()); operLog.setClientIpAddress(request.url().getHost()); operLog.setEventTime(Instant.now()); } /** 处理错误 */ private Mono<ClientResponse> handleError(Throwable e, AuditTrailDto operLog) { operLog.setStatus(500); operLog.setDescription(e.getMessage()); // 保存数据库 // auditTrailService.createAuditTrail(operLog); return Mono.just( ClientResponse.create(HttpStatus.INTERNAL_SERVER_ERROR) .body("{\"error\":\"" + e.getMessage() + "\"}") .build()); } } 优化代码 流式请求 直接返回不记录 响应信息 非流式记录 从而解决接口响应迟钝文件
06-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值