c# 打开excel为乱码
string tmpFileName = Server.MapPath("~") + "\\temp\\" + Guid.NewGuid().ToString() + ".xls";Excel._Application app = new Excel.ApplicationClass(); ///新建进程
app.Visible = false; //设置为后台运行
try
{
Excel.Workbook workbook = app.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet); ///新建一个空白工作薄
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1]; ///获取第一个Sheet
worksheet.Cells.Font.Name = "宋体"; //Sheet 表中的字体
worksheet.Cells.Font.Size = 10; //字体大小
worksheet.PageSetup.LeftMargin = app.InchesToPoints(1); //左边距
worksheet.PageSetup.RightMargin = app.InchesToPoints(0.5); //右边距
worksheet.PageSetup.TopMargin = app.InchesToPoints(0.5); //上边距
worksheet.PageSetup.BottomMargin = app.InchesToPoints(0.5); //下边距
worksheet.PageSetup.PaperSize = Excel.XlPaperSize.xlPaperA4; //纸张为A4
worksheet.PageSetup.Orientation = Excel.XlPageOrientation.xlLandscape; //横向或纵向
worksheet.Name = "内容"; //Sheet名称
//标题
Excel.Range dTitle = worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, 1]);
dTitle.Value2 = "序号";
dTitle.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, null);
dTitle.ColumnWidth = 50;//设置列宽
dTitle.EntireColumn.AutoFit();//自动调整列宽
dTitle.EntireRow.AutoFit();//自动调整行高
dTitle.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
Excel.Range dTitle2 = worksheet.get_Range(worksheet.Cells[1, 2], worksheet.Cells[1, 2]);
dTitle2.Value2 = "时间";
dTitle2.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, null);
dTitle2.ColumnWidth = 100;//设置列宽
dTitle2.EntireColumn.AutoFit();//自动调整列宽
dTitle2.EntireRow.AutoFit();//自动调整行高
dTitle2.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
Excel.Range dTitle3 = worksheet.get_Range(worksheet.Cells[1, 3], worksheet.Cells[1, 3]);
dTitle3.Value2 = "申报单位";
dTitle3.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, null);
dTitle3.ColumnWidth = 200;//设置列宽
dTitle3.EntireColumn.AutoFit();//自动调整列宽
dTitle3.EntireRow.AutoFit();//自动调整行高
Excel.Range r = null;
for (int i = 0; i < table.Tables[0].Rows.Count; i++)
{
r = (Excel.Range)worksheet.Cells[2 + i * 1, 1];
r.Value2 =Convert .ToString ((i + 1));
r.WrapText = true;
r.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, null);
r.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
r = (Excel.Range)worksheet.Cells[2 + i * 1, 2];
r.Value2 = string.Format("{0:d}", table.Tables[0].Rows[i]["doneStepDate"].ToString());
r.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
r.WrapText = true;
r.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, null);
r = (Excel.Range)worksheet.Cells[2 + i * 1, 3];
r.Value2 = table.Tables[0].Rows[i]["app"].ToString();
r.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
r.WrapText = true;
r.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, null);
r.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, null);
}
workbook.SaveAs(tmpFileName, System.Type.Missing, System.Type.Missing, System.Type.Missing,
System.Type.Missing, System.Type.Missing, Excel.XlSaveAsAccessMode.xlShared, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
workbook.Close(System.Type.Missing, System.Type.Missing, System.Type.Missing);
byte[] fileContent = File.ReadAllBytes(tmpFileName);
File.Delete(tmpFileName);
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + HttpUtility.UrlEncode("发改" + DateTime.Now.ToString("yyyyMMdd") + ".xls", System.Text.Encoding.UTF8 ) + "\"");
Response.AddHeader("Content-Type", "application/ms-excel");
Response.ContentEncoding = System.Text.Encoding.UTF8 ;
Response.Charset = "UTF-8";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.HeaderEncoding = System.Text.Encoding.UTF8;
Response.OutputStream.Write(fileContent, 0, fileContent.Length);
Response.Flush();
}
finally
{
if (app != null)
{
app.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject((object)app);
app = null;
GC.Collect();
}
}
这是我的代码,直接保存的话没有乱码,但是用迅雷里的直接打开就是乱码,这是为什么呢,请大家帮我看一下,谢谢了 --------------------编程问答-------------------- 这就没有关系了啊,你给客户用,告诉客户直接下载,就行了,
放心很多的公司都是不允许用迅雷的。 --------------------编程问答-------------------- LZ设置下让程序默认为本地下载不就行了 --------------------编程问答--------------------
这个要怎么设置啊
补充:.NET技术 , ASP.NET