的各个措施总计,txt设置编码格式

By admin in 4858.com on 2019年3月15日

在excel中装置保存之后的编码格式,必要获得到Microsoft.Office.Interop.Excel.Workbook然后设置个中的webOpetions的编码格式就足以了。

第一种:使用 Microsoft.Office.Interop.Excel.dll

第一种:使用 Microsoft.Office.Interop.Excel.dll

最近拍卖Farpoint保存下去的Excel时,发现表格式有各样难点,上面是修改Excel格式操作到剧情,做下笔记
:)

workbook.WebOptions.Encoding = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8;// 设置utf编码格式

率先要求设置 office 的
excel,然后再找到 Microsoft.Office.Interop.Excel.dll 组件,添加到引用。

率先必要设置 office 的
excel,然后再找到 Microsoft.Office.Interop.Excel.dll 组件,添加到引用。

 

在word中装置编码格式,能够一贯在收获到的Microsoft.Office.Interop.Word.Document中安装保存格式。

4858.com 1

4858.com 2

/// <summary>
/// Excel表相关操作
/// </summary>
/// <param name=”firstpath”>第一个Excel表路径</param>
/// <param name=”secondpath”>第二个Excel表路径</param>
private void ExcelOperation(string firstpath, string secondpath)
{
    try
    {
        Microsoft.Office.Interop.Excel.Application App = new
Microsoft.Office.Interop.Excel.Application();
        Microsoft.Office.Interop.Excel.Workbook fWorkbook =
App.Workbooks.Open(firstpath, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing);
        Microsoft.Office.Interop.Excel.Workbook sWorkbook =
App.Workbooks.Open(secondpath, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing);
        App.DisplayAlerts = false;  //屏蔽操作提醒
        try
        {
的各个措施总计,txt设置编码格式。           
((Microsoft.Office.Interop.Excel.Worksheet)sWorkbook.Sheets[1]).Delete(); 
//删除第①个表第1个Sheet
            sWorkbook.Save();

document.SaveEncoding = MsoEncoding.msoEncodingUTF8;// 设置保存的编码格式为utf8

4858.com 34858.com 4

4858.com 54858.com 6

            Microsoft.Office.Interop.Excel.Worksheet sheet =
(Microsoft.Office.Interop.Excel.Worksheet)fWorkbook.Sheets[1]; 
//获取第③个表第一个Sheet

  

public void ExportExcel(DataTable dt)
        {
            if (dt != null)
            {
                Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();

                if (excel == null)
                {
                    return;
                }

                //设置为不可见,操作在后台执行,为 true 的话会打开 Excel
                excel.Visible = false;

                //打开时设置为全屏显式
                //excel.DisplayFullScreen = true;

                //初始化工作簿
                Microsoft.Office.Interop.Excel.Workbooks workbooks = excel.Workbooks;

                //新增加一个工作簿,Add()方法也可以直接传入参数 true
                Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
                //同样是新增一个工作簿,但是会弹出保存对话框
                //Microsoft.Office.Interop.Excel.Workbook workbook = excel.Application.Workbooks.Add(true);

                //新增加一个 Excel 表(sheet)
                Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];

                //设置表的名称
                worksheet.Name = dt.TableName;
                try
                {
                    //创建一个单元格
                    Microsoft.Office.Interop.Excel.Range range;

                    int rowIndex = 1;       //行的起始下标为 1
                    int colIndex = 1;       //列的起始下标为 1

                    //设置列名
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        //设置第一行,即列名
                        worksheet.Cells[rowIndex, colIndex + i] = dt.Columns[i].ColumnName;

                        //获取第一行的每个单元格
                        range = worksheet.Cells[rowIndex, colIndex + i];

                        //设置单元格的内部颜色
                        range.Interior.ColorIndex = 33;

                        //字体加粗
                        range.Font.Bold = true;

                        //设置为黑色
                        range.Font.Color = 0;

                        //设置为宋体
                        range.Font.Name = "Arial";

                        //设置字体大小
                        range.Font.Size = 12;

                        //水平居中
                        range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;

                        //垂直居中
                        range.VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;
                    }

                    //跳过第一行,第一行写入了列名
                    rowIndex++;

                    //写入数据
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        for (int j = 0; j < dt.Columns.Count; j++)
                        {
                            worksheet.Cells[rowIndex + i, colIndex + j] = dt.Rows[i][j].ToString();
                        }
                    }

                    //设置所有列宽为自动列宽
                    //worksheet.Columns.AutoFit();

                    //设置所有单元格列宽为自动列宽
                    worksheet.Cells.Columns.AutoFit();
                    //worksheet.Cells.EntireColumn.AutoFit();

                    //是否提示,如果想删除某个sheet页,首先要将此项设为fasle。
                    excel.DisplayAlerts = false;

                    //保存写入的数据,这里还没有保存到磁盘
                    workbook.Saved = true;

                    //设置导出文件路径
                    string path = HttpContext.Current.Server.MapPath("Export/");

                    //设置新建文件路径及名称
                    string savePath = path + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".xlsx";

                    //创建文件
                    FileStream file = new FileStream(savePath, FileMode.CreateNew);

                    //关闭释放流,不然没办法写入数据
                    file.Close();
                    file.Dispose();

                    //保存到指定的路径
                    workbook.SaveCopyAs(savePath);

                    //还可以加入以下方法输出到浏览器下载
                    FileInfo fileInfo = new FileInfo(savePath);
                    OutputClient(fileInfo);
                }
                catch(Exception ex)
                {

                }
                finally
                {
                    workbook.Close(false, Type.Missing, Type.Missing);
                    workbooks.Close();

                    //关闭退出
                    excel.Quit();

                    //释放 COM 对象
                    Marshal.ReleaseComObject(worksheet);
                    Marshal.ReleaseComObject(workbook);
                    Marshal.ReleaseComObject(workbooks);
                    Marshal.ReleaseComObject(excel);

                    worksheet = null;
                    workbook = null;
                    workbooks = null;
                    excel = null;

                    GC.Collect();
                }
            }
        }
public void ExportExcel(DataTable dt)
        {
            if (dt != null)
            {
                Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();

                if (excel == null)
                {
                    return;
                }

                //设置为不可见,操作在后台执行,为 true 的话会打开 Excel
                excel.Visible = false;

                //打开时设置为全屏显式
                //excel.DisplayFullScreen = true;

                //初始化工作簿
                Microsoft.Office.Interop.Excel.Workbooks workbooks = excel.Workbooks;

                //新增加一个工作簿,Add()方法也可以直接传入参数 true
                Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
                //同样是新增一个工作簿,但是会弹出保存对话框
                //Microsoft.Office.Interop.Excel.Workbook workbook = excel.Application.Workbooks.Add(true);

                //新增加一个 Excel 表(sheet)
                Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];

                //设置表的名称
                worksheet.Name = dt.TableName;
                try
                {
                    //创建一个单元格
                    Microsoft.Office.Interop.Excel.Range range;

                    int rowIndex = 1;       //行的起始下标为 1
                    int colIndex = 1;       //列的起始下标为 1

                    //设置列名
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        //设置第一行,即列名
                        worksheet.Cells[rowIndex, colIndex + i] = dt.Columns[i].ColumnName;

                        //获取第一行的每个单元格
                        range = worksheet.Cells[rowIndex, colIndex + i];

                        //设置单元格的内部颜色
                        range.Interior.ColorIndex = 33;

                        //字体加粗
                        range.Font.Bold = true;

                        //设置为黑色
                        range.Font.Color = 0;

                        //设置为宋体
                        range.Font.Name = "Arial";

                        //设置字体大小
                        range.Font.Size = 12;

                        //水平居中
                        range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;

                        //垂直居中
                        range.VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;
                    }

                    //跳过第一行,第一行写入了列名
                    rowIndex++;

                    //写入数据
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        for (int j = 0; j < dt.Columns.Count; j++)
                        {
                            worksheet.Cells[rowIndex + i, colIndex + j] = dt.Rows[i][j].ToString();
                        }
                    }

                    //设置所有列宽为自动列宽
                    //worksheet.Columns.AutoFit();

                    //设置所有单元格列宽为自动列宽
                    worksheet.Cells.Columns.AutoFit();
                    //worksheet.Cells.EntireColumn.AutoFit();

                    //是否提示,如果想删除某个sheet页,首先要将此项设为fasle。
                    excel.DisplayAlerts = false;

                    //保存写入的数据,这里还没有保存到磁盘
                    workbook.Saved = true;

                    //设置导出文件路径
                    string path = HttpContext.Current.Server.MapPath("Export/");

                    //设置新建文件路径及名称
                    string savePath = path + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".xlsx";

                    //创建文件
                    FileStream file = new FileStream(savePath, FileMode.CreateNew);

                    //关闭释放流,不然没办法写入数据
                    file.Close();
                    file.Dispose();

                    //保存到指定的路径
                    workbook.SaveCopyAs(savePath);

                    //还可以加入以下方法输出到浏览器下载
                    FileInfo fileInfo = new FileInfo(savePath);
                    OutputClient(fileInfo);
                }
                catch(Exception ex)
                {

                }
                finally
                {
                    workbook.Close(false, Type.Missing, Type.Missing);
                    workbooks.Close();

                    //关闭退出
                    excel.Quit();

                    //释放 COM 对象
                    Marshal.ReleaseComObject(worksheet);
                    Marshal.ReleaseComObject(workbook);
                    Marshal.ReleaseComObject(workbooks);
                    Marshal.ReleaseComObject(excel);

                    worksheet = null;
                    workbook = null;
                    workbooks = null;
                    excel = null;

                    GC.Collect();
                }
            }
        }

            sheet.Unprotect(Type.Missing);   //废除工作表爱惜

View Code

View Code

4858.com ,            Microsoft.Office.Interop.Excel.Range range =
(Microsoft.Office.Interop.Excel.Range)sheet.Rows[1, Type.Missing]; 
//获取第①行
            sheet.Activate();
            App.ActiveWindow.FreezePanes = false;  //解除冻结
            App.ActiveWindow.SplitColumn = 0;
            App.ActiveWindow.SplitRow = 0;

4858.com 74858.com 8

4858.com 94858.com 10

            range.Interior.ColorIndex = 0;    //设置第①作为青古铜色
           
range.Delete(Microsoft.Office.Interop.Excel.XlDirection.xlDown);    
//删除第3行

public void OutputClient(FileInfo file)
        {
            HttpContext.Current.Response.Buffer = true;

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.ClearContent();

            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";

            //导出到 .xlsx 格式不能用时,可以试试这个
            //HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

            HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx", DateTime.Now.ToString("yyyy-MM-dd-HH-mm")));

            HttpContext.Current.Response.Charset = "GB2312";
            HttpContext.Current.Response.ContentEncoding = Encoding.GetEncoding("GB2312");

            HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());

            HttpContext.Current.Response.WriteFile(file.FullName);
            HttpContext.Current.Response.Flush();

            HttpContext.Current.Response.Close();
        }
public void OutputClient(FileInfo file)
        {
            HttpContext.Current.Response.Buffer = true;

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.ClearContent();

            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";

            //导出到 .xlsx 格式不能用时,可以试试这个
            //HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

            HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx", DateTime.Now.ToString("yyyy-MM-dd-HH-mm")));

            HttpContext.Current.Response.Charset = "GB2312";
            HttpContext.Current.Response.ContentEncoding = Encoding.GetEncoding("GB2312");

            HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());

            HttpContext.Current.Response.WriteFile(file.FullName);
            HttpContext.Current.Response.Flush();

            HttpContext.Current.Response.Close();
        }

            sheet.Copy(sWorkbook.Sheets[1], Type.Missing); 
//复制到第三个表中
            sWorkbook.Save();

View Code

View Code

        }
        catch (Exception ex)
        {
        }
        finally
        {
            fWorkbook.Close(false, firstpath, null);
           
System.Runtime.InteropServices.Marshal.ReleaseComObject(fWorkbook);
            fWorkbook = null;

第①种艺术质量实在是不敢恭维,而且局限性太多。首先必须求安装
office(假使总结机方面没有的话),而且导出时要求钦点文件保留的路子。也足以出口到浏览器下载,当然前提是早就保存写入数据。

第①种艺术质量实在是不敢恭维,而且局限性太多。首先要求求安装
office(假若总括机方面没有的话),而且导出时索要钦定文件保留的门径。也能够出口到浏览器下载,当然前提是一度保存写入数据。

            sWorkbook.Close(false, secondpath, null);
           
System.Runtime.InteropServices.Marshal.ReleaseComObject(sWorkbook);
            sWorkbook = null;

 

 

            App.Quit();
           
System.Runtime.InteropServices.Marshal.ReleaseComObject(App);
        }
    }
    catch (Exception ex)
    {

第二种:使用 Aspose.Cells.dll

第二种:使用 Aspose.Cells.dll

    }
}

那些 Aspose.Cells 是 Aspose 集团生产的导出 Excel 的控件,不依赖Office,商业软件,收费的。

这么些 Aspose.Cells 是 Aspose 集团推出的导出 Excel 的控件,不正视Office,商业软件,收费的。

能够参照:

能够参见:

4858.com 114858.com 12

4858.com 134858.com 14

public void ExportExcel(DataTable dt)
        {
            try
            {
                //获取指定虚拟路径的物理路径
                string path = HttpContext.Current.Server.MapPath("DLL/") + "License.lic";

                //读取 License 文件
                Stream stream = (Stream)File.OpenRead(path);

                //注册 License
                Aspose.Cells.License li = new Aspose.Cells.License();
                li.SetLicense(stream);

                //创建一个工作簿
                Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();

                //创建一个 sheet 表
                Aspose.Cells.Worksheet worksheet = workbook.Worksheets[0];

                //设置 sheet 表名称
                worksheet.Name = dt.TableName;

                Aspose.Cells.Cell cell;

                int rowIndex = 0;   //行的起始下标为 0
                int colIndex = 0;   //列的起始下标为 0

                //设置列名
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    //获取第一行的每个单元格
                    cell = worksheet.Cells[rowIndex, colIndex + i];

                    //设置列名
                    cell.PutValue(dt.Columns[i].ColumnName);

                    //设置字体
                    cell.Style.Font.Name = "Arial";

                    //设置字体加粗
                    cell.Style.Font.IsBold = true;

                    //设置字体大小
                    cell.Style.Font.Size = 12;

                    //设置字体颜色
                    cell.Style.Font.Color = System.Drawing.Color.Black;

                    //设置背景色
                    cell.Style.BackgroundColor = System.Drawing.Color.LightGreen;
                }

                //跳过第一行,第一行写入了列名
                rowIndex++;

                //写入数据
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        cell = worksheet.Cells[rowIndex + i, colIndex + j];

                        cell.PutValue(dt.Rows[i][j]);
                    }
                }

                //自动列宽
                worksheet.AutoFitColumns();

                //设置导出文件路径
                path = HttpContext.Current.Server.MapPath("Export/");

                //设置新建文件路径及名称
                string savePath = path + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".xlsx";

                //创建文件
                FileStream file = new FileStream(savePath, FileMode.CreateNew);

                //关闭释放流,不然没办法写入数据
                file.Close();
                file.Dispose();

                //保存至指定路径
                workbook.Save(savePath);


                //或者使用下面的方法,输出到浏览器下载。
                //byte[] bytes = workbook.SaveToStream().ToArray();
                //OutputClient(bytes);

                worksheet = null;
                workbook = null;
            }
            catch(Exception ex)
            {

            }
        }
public void ExportExcel(DataTable dt)
        {
            try
            {
                //获取指定虚拟路径的物理路径
                string path = HttpContext.Current.Server.MapPath("DLL/") + "License.lic";

                //读取 License 文件
                Stream stream = (Stream)File.OpenRead(path);

                //注册 License
                Aspose.Cells.License li = new Aspose.Cells.License();
                li.SetLicense(stream);

                //创建一个工作簿
                Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();

                //创建一个 sheet 表
                Aspose.Cells.Worksheet worksheet = workbook.Worksheets[0];

                //设置 sheet 表名称
                worksheet.Name = dt.TableName;

                Aspose.Cells.Cell cell;

                int rowIndex = 0;   //行的起始下标为 0
                int colIndex = 0;   //列的起始下标为 0

                //设置列名
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    //获取第一行的每个单元格
                    cell = worksheet.Cells[rowIndex, colIndex + i];

                    //设置列名
                    cell.PutValue(dt.Columns[i].ColumnName);

                    //设置字体
                    cell.Style.Font.Name = "Arial";

                    //设置字体加粗
                    cell.Style.Font.IsBold = true;

                    //设置字体大小
                    cell.Style.Font.Size = 12;

                    //设置字体颜色
                    cell.Style.Font.Color = System.Drawing.Color.Black;

                    //设置背景色
                    cell.Style.BackgroundColor = System.Drawing.Color.LightGreen;
                }

                //跳过第一行,第一行写入了列名
                rowIndex++;

                //写入数据
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        cell = worksheet.Cells[rowIndex + i, colIndex + j];

                        cell.PutValue(dt.Rows[i][j]);
                    }
                }

                //自动列宽
                worksheet.AutoFitColumns();

                //设置导出文件路径
                path = HttpContext.Current.Server.MapPath("Export/");

                //设置新建文件路径及名称
                string savePath = path + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".xlsx";

                //创建文件
                FileStream file = new FileStream(savePath, FileMode.CreateNew);

                //关闭释放流,不然没办法写入数据
                file.Close();
                file.Dispose();

                //保存至指定路径
                workbook.Save(savePath);


                //或者使用下面的方法,输出到浏览器下载。
                //byte[] bytes = workbook.SaveToStream().ToArray();
                //OutputClient(bytes);

                worksheet = null;
                workbook = null;
            }
            catch(Exception ex)
            {

            }
        }

View Code

View Code

4858.com 154858.com 16

4858.com 174858.com 18

public void OutputClient(byte[] bytes)
        {
            HttpContext.Current.Response.Buffer = true;

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.ClearContent();

            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
            HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xls", DateTime.Now.ToString("yyyy-MM-dd-HH-mm")));

            HttpContext.Current.Response.Charset = "GB2312";
            HttpContext.Current.Response.ContentEncoding = Encoding.GetEncoding("GB2312");

            HttpContext.Current.Response.BinaryWrite(bytes);
            HttpContext.Current.Response.Flush();

            HttpContext.Current.Response.Close();
        }
public void OutputClient(byte[] bytes)
        {
            HttpContext.Current.Response.Buffer = true;

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.ClearContent();

            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
            HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xls", DateTime.Now.ToString("yyyy-MM-dd-HH-mm")));

            HttpContext.Current.Response.Charset = "GB2312";
            HttpContext.Current.Response.ContentEncoding = Encoding.GetEncoding("GB2312");

            HttpContext.Current.Response.BinaryWrite(bytes);
            HttpContext.Current.Response.Flush();

            HttpContext.Current.Response.Close();
        }

View Code

View Code

安装单元格格式为文本方法:

安装单元格格式为文本方法:

cell = worksheet.Cells[rowIndex, colIndex + i];
cell.PutValue(colNames[i]);
style = cell.GetStyle();
style.Number = 49;      // 49(text|@) 表示为文本
cell.SetStyle(style);
cell = worksheet.Cells[rowIndex, colIndex + i];
cell.PutValue(colNames[i]);
style = cell.GetStyle();
style.Number = 49;      // 49(text|@) 表示为文本
cell.SetStyle(style);

其次种办法品质勉强能够,而且操作也不复杂,能够设置导出时文件保留的门道,仍是能够保存为流输出到浏览器下载。

第二种方法质量还不易,而且操作也不复杂,能够安装导出时文件保留的门径,仍是能够保留为流输出到浏览器下载。

 

 

第三种:Microsoft.Jet.OLEDB

第三种:Microsoft.Jet.OLEDB

那种措施操作 Excel 类似于操作数据库。上边先介绍一下两次三番字符串:

那种办法操作 Excel 类似于操作数据库。上边先介绍一下一而再字符串:

// Excel 2003 版本连接字符串
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/xxx.xls;Extended Properties='Excel 8.0;HDR=Yes;IMEX=2;'";

// Excel 2007 以上版本连接字符串
string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:/xxx.xlsx;Extended Properties='Excel 12.0;HDR=Yes;IMEX=2;'";
// Excel 2003 版本连接字符串
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/xxx.xls;Extended Properties='Excel 8.0;HDR=Yes;IMEX=2;'";

// Excel 2007 以上版本连接字符串
string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:/xxx.xlsx;Extended Properties='Excel 12.0;HDR=Yes;IMEX=2;'";

Provider:驱动程序名称

Provider:驱动程序名称

Data Source:钦赐 Excel 文件的门路

Data Source:钦赐 Excel 文件的路线

Extended Properties:Excel 8.0 针对 Excel 三千 及以上版本;Excel 12.0
针对 Excel 二〇〇五 及以上版本。

Extended Properties:Excel 8.0 针对 Excel 三千 及以上版本;Excel 12.0
针对 Excel 二零零六 及以上版本。

HD奥迪Q3:Yes 表示第叁行包含列名,在总计行数时就不带有第贰行。NO 则统统相反。

HD路虎极光:Yes 代表第①行李包裹罗列名,在测算行数时就不含有第贰行。NO 则完全相反。

IMEX:0 写入方式;1 读取形式;2 读写方式。如若报错为“不能够修改表 sheet1
的宏图。它在只读数据库中”,那就去掉那几个,难题一下子就解决了。

IMEX:0 写入形式;1 读取情势;2 读写情势。要是报错为“没办法修改表 sheet1
的宏图。它在只读数据库中”,那就去掉那几个,问题解决。

4858.com 194858.com 20

4858.com 214858.com 22

public void ExportExcel(DataTable dt)
        {
            OleDbConnection conn = null;
            OleDbCommand cmd = null;

            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();

            Microsoft.Office.Interop.Excel.Workbooks workbooks = excel.Workbooks;

            Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(true);

            try
            {
                //设置区域为当前线程的区域
                dt.Locale = System.Threading.Thread.CurrentThread.CurrentCulture;

                //设置导出文件路径
                string path = HttpContext.Current.Server.MapPath("Export/");

                //设置新建文件路径及名称
                string savePath = path + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".xlsx";

                //创建文件
                FileStream file = new FileStream(savePath, FileMode.CreateNew);

                //关闭释放流,不然没办法写入数据
                file.Close();
                file.Dispose();

                //由于使用流创建的 excel 文件不能被正常识别,所以只能使用这种方式另存为一下。
                workbook.SaveCopyAs(savePath);


                // Excel 2003 版本连接字符串
                //string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + savePath + "';Extended Properties='Excel 8.0;HDR=Yes;'";

                // Excel 2007 以上版本连接字符串
                string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='"+ savePath + "';Extended Properties='Excel 12.0;HDR=Yes;'";

                //创建连接对象
                conn = new OleDbConnection(strConn);
                //打开连接
                conn.Open();

                //创建命令对象
                cmd = conn.CreateCommand();

                //获取 excel 所有的数据表。
                //new object[] { null, null, null, "Table" }指定返回的架构信息:参数介绍
                //第一个参数指定目录
                //第二个参数指定所有者
                //第三个参数指定表名
                //第四个参数指定表类型
                DataTable dtSheetName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });

                //因为后面创建的表都会在最后面,所以本想删除掉前面的表,结果发现做不到,只能清空数据。
                for (int i = 0; i < dtSheetName.Rows.Count; i++)
                {
                    cmd.CommandText = "drop table [" + dtSheetName.Rows[i]["TABLE_NAME"].ToString() + "]";
                    cmd.ExecuteNonQuery();
                }

                //添加一个表,即 Excel 中 sheet 表
                cmd.CommandText = "create table " + dt.TableName + " ([S_Id] INT,[S_StuNo] VarChar,[S_Name] VarChar,[S_Sex] VarChar,[S_Height] VarChar,[S_BirthDate] VarChar,[C_S_Id] INT)";
                cmd.ExecuteNonQuery();

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string values = "";

                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        values += "'" + dt.Rows[i][j].ToString() + "',";
                    }

                    //判断最后一个字符是否为逗号,如果是就截取掉
                    if (values.LastIndexOf(',') == values.Length - 1)
                    {
                        values = values.Substring(0, values.Length - 1);
                    }

                    //写入数据
                    cmd.CommandText = "insert into " + dt.TableName + " (S_Id,S_StuNo,S_Name,S_Sex,S_Height,S_BirthDate,C_S_Id) values (" + values + ")";
                    cmd.ExecuteNonQuery();
                }

                conn.Close();
                conn.Dispose();
                cmd.Dispose();

                //加入下面的方法,把保存的 Excel 文件输出到浏览器下载。需要先关闭连接。
                FileInfo fileInfo = new FileInfo(savePath);
                OutputClient(fileInfo);
            }
            catch (Exception ex)
            {

            }
            finally
            {
                workbook.Close(false, Type.Missing, Type.Missing);
                workbooks.Close();
                excel.Quit();

                Marshal.ReleaseComObject(workbook);
                Marshal.ReleaseComObject(workbooks);
                Marshal.ReleaseComObject(excel);

                workbook = null;
                workbooks = null;
                excel = null;

                GC.Collect();
            }
        }
public void ExportExcel(DataTable dt)
        {
            OleDbConnection conn = null;
            OleDbCommand cmd = null;

            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();

            Microsoft.Office.Interop.Excel.Workbooks workbooks = excel.Workbooks;

            Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(true);

            try
            {
                //设置区域为当前线程的区域
                dt.Locale = System.Threading.Thread.CurrentThread.CurrentCulture;

                //设置导出文件路径
                string path = HttpContext.Current.Server.MapPath("Export/");

                //设置新建文件路径及名称
                string savePath = path + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".xlsx";

                //创建文件
                FileStream file = new FileStream(savePath, FileMode.CreateNew);

                //关闭释放流,不然没办法写入数据
                file.Close();
                file.Dispose();

                //由于使用流创建的 excel 文件不能被正常识别,所以只能使用这种方式另存为一下。
                workbook.SaveCopyAs(savePath);


                // Excel 2003 版本连接字符串
                //string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + savePath + "';Extended Properties='Excel 8.0;HDR=Yes;'";

                // Excel 2007 以上版本连接字符串
                string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='"+ savePath + "';Extended Properties='Excel 12.0;HDR=Yes;'";

                //创建连接对象
                conn = new OleDbConnection(strConn);
                //打开连接
                conn.Open();

                //创建命令对象
                cmd = conn.CreateCommand();

                //获取 excel 所有的数据表。
                //new object[] { null, null, null, "Table" }指定返回的架构信息:参数介绍
                //第一个参数指定目录
                //第二个参数指定所有者
                //第三个参数指定表名
                //第四个参数指定表类型
                DataTable dtSheetName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });

                //因为后面创建的表都会在最后面,所以本想删除掉前面的表,结果发现做不到,只能清空数据。
                for (int i = 0; i < dtSheetName.Rows.Count; i++)
                {
                    cmd.CommandText = "drop table [" + dtSheetName.Rows[i]["TABLE_NAME"].ToString() + "]";
                    cmd.ExecuteNonQuery();
                }

                //添加一个表,即 Excel 中 sheet 表
                cmd.CommandText = "create table " + dt.TableName + " ([S_Id] INT,[S_StuNo] VarChar,[S_Name] VarChar,[S_Sex] VarChar,[S_Height] VarChar,[S_BirthDate] VarChar,[C_S_Id] INT)";
                cmd.ExecuteNonQuery();

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string values = "";

                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        values += "'" + dt.Rows[i][j].ToString() + "',";
                    }

                    //判断最后一个字符是否为逗号,如果是就截取掉
                    if (values.LastIndexOf(',') == values.Length - 1)
                    {
                        values = values.Substring(0, values.Length - 1);
                    }

                    //写入数据
                    cmd.CommandText = "insert into " + dt.TableName + " (S_Id,S_StuNo,S_Name,S_Sex,S_Height,S_BirthDate,C_S_Id) values (" + values + ")";
                    cmd.ExecuteNonQuery();
                }

                conn.Close();
                conn.Dispose();
                cmd.Dispose();

                //加入下面的方法,把保存的 Excel 文件输出到浏览器下载。需要先关闭连接。
                FileInfo fileInfo = new FileInfo(savePath);
                OutputClient(fileInfo);
            }
            catch (Exception ex)
            {

            }
            finally
            {
                workbook.Close(false, Type.Missing, Type.Missing);
                workbooks.Close();
                excel.Quit();

                Marshal.ReleaseComObject(workbook);
                Marshal.ReleaseComObject(workbooks);
                Marshal.ReleaseComObject(excel);

                workbook = null;
                workbooks = null;
                excel = null;

                GC.Collect();
            }
        }

View Code

View Code

4858.com 234858.com 24

4858.com 254858.com 26

public void OutputClient(FileInfo file)
        {
            HttpResponse response = HttpContext.Current.Response;

            response.Buffer = true;

            response.Clear();
            response.ClearHeaders();
            response.ClearContent();

            response.ContentType = "application/vnd.ms-excel";

            //导出到 .xlsx 格式不能用时,可以试试这个
            //HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

            response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx", DateTime.Now.ToString("yyyy-MM-dd-HH-mm")));

            response.Charset = "GB2312";
            response.ContentEncoding = Encoding.GetEncoding("GB2312");

            response.AddHeader("Content-Length", file.Length.ToString());

            response.WriteFile(file.FullName);
            response.Flush();

            response.Close();
        }
public void OutputClient(FileInfo file)
        {
            HttpResponse response = HttpContext.Current.Response;

            response.Buffer = true;

            response.Clear();
            response.ClearHeaders();
            response.ClearContent();

            response.ContentType = "application/vnd.ms-excel";

            //导出到 .xlsx 格式不能用时,可以试试这个
            //HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

            response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx", DateTime.Now.ToString("yyyy-MM-dd-HH-mm")));

            response.Charset = "GB2312";
            response.ContentEncoding = Encoding.GetEncoding("GB2312");

            response.AddHeader("Content-Length", file.Length.ToString());

            response.WriteFile(file.FullName);
            response.Flush();

            response.Close();
        }

View Code

View Code

那种格局必要内定2个曾经存在的 Excel
文件作为写入数据的模版,不然的话就得利用流创设一个新的 Excel
文件,不过那样是无奈识其余,那就供给用到 Microsoft.Office.Interop.Excel.dll
里面包车型大巴 Microsoft.Office.Interop.Excel.Workbook.SaveCopyAs()
方法另存为一下,那样质量也就更差了。

那种方法供给内定多个已经存在的 Excel
文件作为写入数据的沙盘,不然的话就得利用流创设1个新的 Excel
文件,可是如此是无法识别的,那就需求用到 Microsoft.Office.Interop.Excel.dll
里面包车型客车 Microsoft.Office.Interop.Excel.Workbook.SaveCopyAs()
方法另存为一下,那样质量也就更差了。

行使操作命令创设的表都是在最后面包车型客车,前边的也无法删除(笔者是没有找到办法),当然也足以不更创立,直接写入数据也可以。

运用操作命令成立的表都以在结尾面包车型地铁,前边的也无可怎样删除(笔者是没有找到办法),当然也能够不再成立,直接写入数据也得以。

 

 

第四种:NPOI

第四种:NPOI

NPOI 是 POI 项指标.NET版本,它不选择Office COM 组件,不供给设置 Microsoft Office,近日补助 Office 二零零一 和
二〇〇七 版本。

NPOI 是 POI 项目标.NET版本,它不行使
Office COM 组件,不须要安装 Microsoft Office,最近支撑 Office 二〇〇〇 和
2005 版本。

NPOI 是免费开源的,操作也正如便于,下载地址:

NPOI 是免费开源的,操作也相比较方便,下载地址:

4858.com 274858.com 28

4858.com 294858.com 30

public void ExportExcel(DataTable dt)
        {
            try
            {
                //创建一个工作簿
                IWorkbook workbook = new HSSFWorkbook();

                //创建一个 sheet 表
                ISheet sheet = workbook.CreateSheet(dt.TableName);

                //创建一行
                IRow rowH = sheet.CreateRow(0);

                //创建一个单元格
                ICell cell = null;

                //创建单元格样式
                ICellStyle cellStyle = workbook.CreateCellStyle();

                //创建格式
                IDataFormat dataFormat = workbook.CreateDataFormat();

                //设置为文本格式,也可以为 text,即 dataFormat.GetFormat("text");
                cellStyle.DataFormat = dataFormat.GetFormat("@");

                //设置列名
                foreach (DataColumn col in dt.Columns)
                {
                    //创建单元格并设置单元格内容
                    rowH.CreateCell(col.Ordinal).SetCellValue(col.Caption);

                    //设置单元格格式
                    rowH.Cells[col.Ordinal].CellStyle = cellStyle;
                }

                //写入数据
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    //跳过第一行,第一行为列名
                    IRow row = sheet.CreateRow(i + 1);

                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        cell = row.CreateCell(j);
                        cell.SetCellValue(dt.Rows[i][j].ToString());
                        cell.CellStyle = cellStyle;
                    }
                }

                //设置导出文件路径
                string path = HttpContext.Current.Server.MapPath("Export/");

                //设置新建文件路径及名称
                string savePath = path + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".xls";

                //创建文件
                FileStream file = new FileStream(savePath, FileMode.CreateNew,FileAccess.Write);

                //创建一个 IO 流
                MemoryStream ms = new MemoryStream();

                //写入到流
                workbook.Write(ms);

                //转换为字节数组
                byte[] bytes = ms.ToArray();

                file.Write(bytes, 0, bytes.Length);
                file.Flush();

                //还可以调用下面的方法,把流输出到浏览器下载
                OutputClient(bytes);

                //释放资源
                bytes = null;

                ms.Close();
                ms.Dispose();

                file.Close();
                file.Dispose();

                workbook.Close();
                sheet = null;
                workbook = null;
            }
            catch(Exception ex)
            {

            }
        }
public void ExportExcel(DataTable dt)
        {
            try
            {
                //创建一个工作簿
                IWorkbook workbook = new HSSFWorkbook();

                //创建一个 sheet 表
                ISheet sheet = workbook.CreateSheet(dt.TableName);

                //创建一行
                IRow rowH = sheet.CreateRow(0);

                //创建一个单元格
                ICell cell = null;

                //创建单元格样式
                ICellStyle cellStyle = workbook.CreateCellStyle();

                //创建格式
                IDataFormat dataFormat = workbook.CreateDataFormat();

                //设置为文本格式,也可以为 text,即 dataFormat.GetFormat("text");
                cellStyle.DataFormat = dataFormat.GetFormat("@");

                //设置列名
                foreach (DataColumn col in dt.Columns)
                {
                    //创建单元格并设置单元格内容
                    rowH.CreateCell(col.Ordinal).SetCellValue(col.Caption);

                    //设置单元格格式
                    rowH.Cells[col.Ordinal].CellStyle = cellStyle;
                }

                //写入数据
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    //跳过第一行,第一行为列名
                    IRow row = sheet.CreateRow(i + 1);

                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        cell = row.CreateCell(j);
                        cell.SetCellValue(dt.Rows[i][j].ToString());
                        cell.CellStyle = cellStyle;
                    }
                }

                //设置导出文件路径
                string path = HttpContext.Current.Server.MapPath("Export/");

                //设置新建文件路径及名称
                string savePath = path + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".xls";

                //创建文件
                FileStream file = new FileStream(savePath, FileMode.CreateNew,FileAccess.Write);

                //创建一个 IO 流
                MemoryStream ms = new MemoryStream();

                //写入到流
                workbook.Write(ms);

                //转换为字节数组
                byte[] bytes = ms.ToArray();

                file.Write(bytes, 0, bytes.Length);
                file.Flush();

                //还可以调用下面的方法,把流输出到浏览器下载
                OutputClient(bytes);

                //释放资源
                bytes = null;

                ms.Close();
                ms.Dispose();

                file.Close();
                file.Dispose();

                workbook.Close();
                sheet = null;
                workbook = null;
            }
            catch(Exception ex)
            {

            }
        }

View Code

View Code

4858.com 314858.com 32

4858.com 334858.com 34

public void OutputClient(byte[] bytes)
        {
            HttpResponse response = HttpContext.Current.Response;

            response.Buffer = true;

            response.Clear();
            response.ClearHeaders();
            response.ClearContent();

            response.ContentType = "application/vnd.ms-excel";
            response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xls", DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")));

            response.Charset = "GB2312";
            response.ContentEncoding = Encoding.GetEncoding("GB2312");

            response.BinaryWrite(bytes);
            response.Flush();

            response.Close();
        }
public void OutputClient(byte[] bytes)
        {
            HttpResponse response = HttpContext.Current.Response;

            response.Buffer = true;

            response.Clear();
            response.ClearHeaders();
            response.ClearContent();

            response.ContentType = "application/vnd.ms-excel";
            response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xls", DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")));

            response.Charset = "GB2312";
            response.ContentEncoding = Encoding.GetEncoding("GB2312");

            response.BinaryWrite(bytes);
            response.Flush();

            response.Close();
        }

View Code

View Code

// 2007 版本创建一个工作簿
IWorkbook workbook = new XSSFWorkbook();

// 2003 版本创建一个工作簿
IWorkbook workbook = new HSSFWorkbook();
// 2007 版本创建一个工作簿
IWorkbook workbook = new XSSFWorkbook();

// 2003 版本创建一个工作簿
IWorkbook workbook = new HSSFWorkbook();

PS:操作 二零零三 版本需求添加 NPOI.dll 的引用,操作 二零零五 版本须求添加
NPOI.OOXML.dll 的引用。

PS:操作 二零零一 版本必要添加 NPOI.dll 的引用,操作 二零零六 版本需求添加
NPOI.OOXML.dll 的引用。

 

 

第五种:GridView

第五种:GridView

直白动用 GridView 把 DataTable
的数量转换为字符串流,然后输出到浏览器下载。

直接运用 GridView 把 DataTable
的多少转换为字符串流,然后输出到浏览器下载。

4858.com 354858.com 36

4858.com 374858.com 38

public void ExportExcel(DataTable dt)
        {
            HttpResponse response = HttpContext.Current.Response;

            response.Buffer = true;

            response.Clear();
            response.ClearHeaders();
            response.ClearContent();

            response.ContentType = "application/vnd.ms-excel";
            response.AddHeader("content-disposition", string.Format("attachment; filename={0}.xls", DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")));

            response.Charset = "GB2312";
            response.ContentEncoding = Encoding.GetEncoding("GB2312");

            //实例化一个流
            StringWriter stringWrite = new StringWriter();

            //指定文本输出到流
            HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

            GridView gv = new GridView();
            gv.DataSource = dt;
            gv.DataBind();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    //设置每个单元格的格式
                    gv.Rows[i].Cells[j].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
                }
            }

            //把 GridView 的内容输出到 HtmlTextWriter
            gv.RenderControl(htmlWrite);

            response.Write(stringWrite.ToString());
            response.Flush();

            response.Close();
        }
public void ExportExcel(DataTable dt)
        {
            HttpResponse response = HttpContext.Current.Response;

            response.Buffer = true;

            response.Clear();
            response.ClearHeaders();
            response.ClearContent();

            response.ContentType = "application/vnd.ms-excel";
            response.AddHeader("content-disposition", string.Format("attachment; filename={0}.xls", DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")));

            response.Charset = "GB2312";
            response.ContentEncoding = Encoding.GetEncoding("GB2312");

            //实例化一个流
            StringWriter stringWrite = new StringWriter();

            //指定文本输出到流
            HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

            GridView gv = new GridView();
            gv.DataSource = dt;
            gv.DataBind();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    //设置每个单元格的格式
                    gv.Rows[i].Cells[j].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
                }
            }

            //把 GridView 的内容输出到 HtmlTextWriter
            gv.RenderControl(htmlWrite);

            response.Write(stringWrite.ToString());
            response.Flush();

            response.Close();
        }

View Code

View Code

那种措施导出 .xlsx 格式的 Excel 文件时,不能打开。导出 .xls
格式的,会唤醒文件格式和扩展名不匹配,可是足以打开的。

那种方法导出 .xlsx 格式的 Excel 文件时,不能打开。导出 .xls
格式的,会唤醒文件格式和扩展名不匹配,可是能够打开的。

 

 

第六种:DataGrid

第六种:DataGrid

其实这一种方法和上边的那一种办法差不多是一致的。

实际上这一种办法和方面的那一种艺术大约是如出一辙的。

4858.com 394858.com 40

4858.com 414858.com 42

public void ExportExcel(DataTable dt)
        {
            HttpResponse response = HttpContext.Current.Response;

            response.Buffer = true;

            response.Clear();
            response.ClearHeaders();
            response.ClearContent();

            response.ContentType = "application/vnd.ms-excel";
            response.AddHeader("content-disposition", string.Format("attachment; filename={0}.xls", DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")));

            response.Charset = "GB2312";
            response.ContentEncoding = Encoding.GetEncoding("GB2312");

            //实例化一个流
            StringWriter stringWrite = new StringWriter();

            //指定文本输出到流
            HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

            DataGrid dg = new DataGrid();
            dg.DataSource = dt;
            dg.DataBind();

            dg.Attributes.Add("style", "vnd.ms-excel.numberformat:@");

            //把 DataGrid 的内容输出到 HtmlTextWriter
            dg.RenderControl(htmlWrite);

            response.Write(stringWrite.ToString());
            response.Flush();

            response.Close();
        }
public void ExportExcel(DataTable dt)
        {
            HttpResponse response = HttpContext.Current.Response;

            response.Buffer = true;

            response.Clear();
            response.ClearHeaders();
            response.ClearContent();

            response.ContentType = "application/vnd.ms-excel";
            response.AddHeader("content-disposition", string.Format("attachment; filename={0}.xls", DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")));

            response.Charset = "GB2312";
            response.ContentEncoding = Encoding.GetEncoding("GB2312");

            //实例化一个流
            StringWriter stringWrite = new StringWriter();

            //指定文本输出到流
            HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

            DataGrid dg = new DataGrid();
            dg.DataSource = dt;
            dg.DataBind();

            dg.Attributes.Add("style", "vnd.ms-excel.numberformat:@");

            //把 DataGrid 的内容输出到 HtmlTextWriter
            dg.RenderControl(htmlWrite);

            response.Write(stringWrite.ToString());
            response.Flush();

            response.Close();
        }

View Code

View Code

 

 

第多样:直接利用 IO 流

第⑧种:间接行使 IO 流

第①种艺术,使用文件流在磁盘创立二个 Excel 文件,然后利用流写入数据。

第1种格局,使用文件流在磁盘创造一个 Excel 文件,然后使用流写入数据。

4858.com 434858.com 44

4858.com 454858.com 46

public void ExportExcel(DataTable dt)
        {
            //设置导出文件路径
            string path = HttpContext.Current.Server.MapPath("Export/");

            //设置新建文件路径及名称
            string savePath = path + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".xls";

            //创建文件
            FileStream file = new FileStream(savePath, FileMode.CreateNew, FileAccess.Write);

            //以指定的字符编码向指定的流写入字符
            StreamWriter sw = new StreamWriter(file, Encoding.GetEncoding("GB2312"));

            StringBuilder strbu = new StringBuilder();

            //写入标题
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                strbu.Append(dt.Columns[i].ColumnName.ToString() + "\t");
            }
            //加入换行字符串
            strbu.Append(Environment.NewLine);

            //写入内容
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    strbu.Append(dt.Rows[i][j].ToString() + "\t");
                }
                strbu.Append(Environment.NewLine);
            }

            sw.Write(strbu.ToString());
            sw.Flush();
            file.Flush();

            sw.Close();
            sw.Dispose();

            file.Close();
            file.Dispose();
        }
public void ExportExcel(DataTable dt)
        {
            //设置导出文件路径
            string path = HttpContext.Current.Server.MapPath("Export/");

            //设置新建文件路径及名称
            string savePath = path + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".xls";

            //创建文件
            FileStream file = new FileStream(savePath, FileMode.CreateNew, FileAccess.Write);

            //以指定的字符编码向指定的流写入字符
            StreamWriter sw = new StreamWriter(file, Encoding.GetEncoding("GB2312"));

            StringBuilder strbu = new StringBuilder();

            //写入标题
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                strbu.Append(dt.Columns[i].ColumnName.ToString() + "\t");
            }
            //加入换行字符串
            strbu.Append(Environment.NewLine);

            //写入内容
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    strbu.Append(dt.Rows[i][j].ToString() + "\t");
                }
                strbu.Append(Environment.NewLine);
            }

            sw.Write(strbu.ToString());
            sw.Flush();
            file.Flush();

            sw.Close();
            sw.Dispose();

            file.Close();
            file.Dispose();
        }

View Code

View Code

其次种方法,那种方法就不须求在地面磁盘创设文件了,首先创建一个内部存款和储蓄器流写入数据,然后输出到浏览器下载。

第二种艺术,那种办法就不须要在地面磁盘创设文件了,首先成立3个内部存款和储蓄器流写入数据,然后输出到浏览器下载。

4858.com 474858.com 48

4858.com 494858.com 50

public void ExportExcel(DataTable dt)
        {
            //创建一个内存流
            MemoryStream ms = new MemoryStream();

            //以指定的字符编码向指定的流写入字符
            StreamWriter sw = new StreamWriter(ms, Encoding.GetEncoding("GB2312"));

            StringBuilder strbu = new StringBuilder();

            //写入标题
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                strbu.Append(dt.Columns[i].ColumnName.ToString() + "\t");
            }
            //加入换行字符串
            strbu.Append(Environment.NewLine);

            //写入内容
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    strbu.Append(dt.Rows[i][j].ToString() + "\t");
                }
                strbu.Append(Environment.NewLine);
            }

            sw.Write(strbu.ToString());
            sw.Flush();

            sw.Close();
            sw.Dispose();

            //转换为字节数组
            byte[] bytes = ms.ToArray();

            ms.Close();
            ms.Dispose();

            OutputClient(bytes);
        }
public void ExportExcel(DataTable dt)
        {
            //创建一个内存流
            MemoryStream ms = new MemoryStream();

            //以指定的字符编码向指定的流写入字符
            StreamWriter sw = new StreamWriter(ms, Encoding.GetEncoding("GB2312"));

            StringBuilder strbu = new StringBuilder();

            //写入标题
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                strbu.Append(dt.Columns[i].ColumnName.ToString() + "\t");
            }
            //加入换行字符串
            strbu.Append(Environment.NewLine);

            //写入内容
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    strbu.Append(dt.Rows[i][j].ToString() + "\t");
                }
                strbu.Append(Environment.NewLine);
            }

            sw.Write(strbu.ToString());
            sw.Flush();

            sw.Close();
            sw.Dispose();

            //转换为字节数组
            byte[] bytes = ms.ToArray();

            ms.Close();
            ms.Dispose();

            OutputClient(bytes);
        }

View Code

View Code

4858.com 514858.com 52

4858.com 534858.com 54

public void OutputClient(byte[] bytes)
        {
            HttpResponse response = HttpContext.Current.Response;

            response.Buffer = true;

            response.Clear();
            response.ClearHeaders();
            response.ClearContent();

            response.ContentType = "application/vnd.ms-excel";
            response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xls", DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")));

            response.Charset = "GB2312";
            response.ContentEncoding = Encoding.GetEncoding("GB2312");

            response.BinaryWrite(bytes);
            response.Flush();

            response.Close();
        }
public void OutputClient(byte[] bytes)
        {
            HttpResponse response = HttpContext.Current.Response;

            response.Buffer = true;

            response.Clear();
            response.ClearHeaders();
            response.ClearContent();

            response.ContentType = "application/vnd.ms-excel";
            response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xls", DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")));

            response.Charset = "GB2312";
            response.ContentEncoding = Encoding.GetEncoding("GB2312");

            response.BinaryWrite(bytes);
            response.Flush();

            response.Close();
        }

View Code

View Code

那种措施有一个弊端,便是不可能设置单元格的格式(至少小编是一直不找到,是在下输了)。

那种措施有一个害处,正是不可能安装单元格的格式(至少小编是从未找到,是在下输了)。

 

 

第八种:EPPlus

第八种:EPPlus

EPPlus 是一个运用 Open Office Xml 格式(xlsx)读取和写入 Excel 二〇〇五/二〇〇八文件的 .net 库,并且免费开源。

EPPlus 是二个应用 Open Office Xml 格式(xlsx)读取和写入 Excel 二零零五/二零零六文件的 .net 库,并且免费开源。

下载地址:

下载地址:

先是种方法,先使用文件流在磁盘创立2个 Excel
文件,然后打开那些文件写入数据并保存。

率先种艺术,先选择文件流在磁盘创造三个 Excel
文件,然后打开那个文件写入数据并保存。

4858.com 554858.com 56

4858.com 574858.com 58

public void ExportExcel(DataTable dt)
        {
            //新建一个 Excel 文件
            string path = HttpContext.Current.Server.MapPath("Export/");
            string filePath = path + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".xlsx";
            FileStream fileStream = new FileStream(filePath, FileMode.Create);

            //加载这个 Excel 文件
            ExcelPackage package = new ExcelPackage(fileStream);

            // 添加一个 sheet 表
            ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(dt.TableName);

            int rowIndex = 1;   // 起始行为 1
            int colIndex = 1;   // 起始列为 1

            //设置列名
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                worksheet.Cells[rowIndex, colIndex + i].Value = dt.Columns[i].ColumnName;

                //自动调整列宽,也可以指定最小宽度和最大宽度
                worksheet.Column(colIndex + i).AutoFit();
            }

            // 跳过第一列列名
            rowIndex++;

            //写入数据
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    worksheet.Cells[rowIndex + i, colIndex + j].Value = dt.Rows[i][j].ToString();
                }

                //自动调整行高
                worksheet.Row(rowIndex + i).CustomHeight = true;
            }

            //设置字体,也可以是中文,比如:宋体
            worksheet.Cells.Style.Font.Name = "Arial";

            //字体加粗
            worksheet.Cells.Style.Font.Bold = true;

            //字体大小
            worksheet.Cells.Style.Font.Size = 12;

            //字体颜色
            worksheet.Cells.Style.Font.Color.SetColor(System.Drawing.Color.Black);

            //单元格背景样式,要设置背景颜色必须先设置背景样式
            worksheet.Cells.Style.Fill.PatternType = ExcelFillStyle.Solid;
            //单元格背景颜色
            worksheet.Cells.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.DimGray);

            //设置单元格所有边框样式和颜色
            worksheet.Cells.Style.Border.BorderAround(ExcelBorderStyle.Thin, System.Drawing.ColorTranslator.FromHtml("#0097DD"));

            //单独设置单元格四边框 Top、Bottom、Left、Right 的样式和颜色
            //worksheet.Cells.Style.Border.Top.Style = ExcelBorderStyle.Thin;
            //worksheet.Cells.Style.Border.Top.Color.SetColor(System.Drawing.Color.Black);

            //垂直居中
            worksheet.Cells.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

            //水平居中
            worksheet.Cells.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

            //单元格是否自动换行
            worksheet.Cells.Style.WrapText = false;

            //设置单元格格式为文本
            worksheet.Cells.Style.Numberformat.Format = "@";

            //单元格自动适应大小
            worksheet.Cells.Style.ShrinkToFit = true;

            package.Save();

            fileStream.Close();
            fileStream.Dispose();

            worksheet.Dispose();
            package.Dispose();
        }
public void ExportExcel(DataTable dt)
        {
            //新建一个 Excel 文件
            string path = HttpContext.Current.Server.MapPath("Export/");
            string filePath = path + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".xlsx";
            FileStream fileStream = new FileStream(filePath, FileMode.Create);

            //加载这个 Excel 文件
            ExcelPackage package = new ExcelPackage(fileStream);

            // 添加一个 sheet 表
            ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(dt.TableName);

            int rowIndex = 1;   // 起始行为 1
            int colIndex = 1;   // 起始列为 1

            //设置列名
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                worksheet.Cells[rowIndex, colIndex + i].Value = dt.Columns[i].ColumnName;

                //自动调整列宽,也可以指定最小宽度和最大宽度
                worksheet.Column(colIndex + i).AutoFit();
            }

            // 跳过第一列列名
            rowIndex++;

            //写入数据
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    worksheet.Cells[rowIndex + i, colIndex + j].Value = dt.Rows[i][j].ToString();
                }

                //自动调整行高
                worksheet.Row(rowIndex + i).CustomHeight = true;
            }

            //设置字体,也可以是中文,比如:宋体
            worksheet.Cells.Style.Font.Name = "Arial";

            //字体加粗
            worksheet.Cells.Style.Font.Bold = true;

            //字体大小
            worksheet.Cells.Style.Font.Size = 12;

            //字体颜色
            worksheet.Cells.Style.Font.Color.SetColor(System.Drawing.Color.Black);

            //单元格背景样式,要设置背景颜色必须先设置背景样式
            worksheet.Cells.Style.Fill.PatternType = ExcelFillStyle.Solid;
            //单元格背景颜色
            worksheet.Cells.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.DimGray);

            //设置单元格所有边框样式和颜色
            worksheet.Cells.Style.Border.BorderAround(ExcelBorderStyle.Thin, System.Drawing.ColorTranslator.FromHtml("#0097DD"));

            //单独设置单元格四边框 Top、Bottom、Left、Right 的样式和颜色
            //worksheet.Cells.Style.Border.Top.Style = ExcelBorderStyle.Thin;
            //worksheet.Cells.Style.Border.Top.Color.SetColor(System.Drawing.Color.Black);

            //垂直居中
            worksheet.Cells.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

            //水平居中
            worksheet.Cells.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

            //单元格是否自动换行
            worksheet.Cells.Style.WrapText = false;

            //设置单元格格式为文本
            worksheet.Cells.Style.Numberformat.Format = "@";

            //单元格自动适应大小
            worksheet.Cells.Style.ShrinkToFit = true;

            package.Save();

            fileStream.Close();
            fileStream.Dispose();

            worksheet.Dispose();
            package.Dispose();
        }

View Code

View Code

第三种方法,不钦命文件,先成立多个 Excel
工作簿写入数据以后,能够挑选保存至钦命文件(新建文件)、保存为流、获取字节数组输出到浏览器下载。

第③种办法,不钦赐文件,先创建1个 Excel
工作簿写入数据今后,能够挑选保存至内定文件(新建文件)、保存为流、获取字节数组输出到浏览器下载。

4858.com 594858.com 60

4858.com 614858.com 62

public void ExportExcel(DataTable dt)
        {
            //新建一个 Excel 工作簿
            ExcelPackage package = new ExcelPackage();

            // 添加一个 sheet 表
            ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(dt.TableName);

            int rowIndex = 1;   // 起始行为 1
            int colIndex = 1;   // 起始列为 1

            //设置列名
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                worksheet.Cells[rowIndex, colIndex + i].Value = dt.Columns[i].ColumnName;

                //自动调整列宽,也可以指定最小宽度和最大宽度
                worksheet.Column(colIndex + i).AutoFit();
            }

            // 跳过第一列列名
            rowIndex++;

            //写入数据
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    worksheet.Cells[rowIndex + i, colIndex + j].Value = dt.Rows[i][j].ToString();
                }

                //自动调整行高
                worksheet.Row(rowIndex + i).CustomHeight = true;
            }

            //设置字体,也可以是中文,比如:宋体
            worksheet.Cells.Style.Font.Name = "Arial";

            //字体加粗
            worksheet.Cells.Style.Font.Bold = true;

            //字体大小
            worksheet.Cells.Style.Font.Size = 12;

            //字体颜色
            worksheet.Cells.Style.Font.Color.SetColor(System.Drawing.Color.Black);

            //单元格背景样式,要设置背景颜色必须先设置背景样式
            worksheet.Cells.Style.Fill.PatternType = ExcelFillStyle.Solid;
            //单元格背景颜色
            worksheet.Cells.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.DimGray);

            //设置单元格所有边框样式和颜色
            worksheet.Cells.Style.Border.BorderAround(ExcelBorderStyle.Thin, System.Drawing.ColorTranslator.FromHtml("#0097DD"));

            //单独设置单元格四边框 Top、Bottom、Left、Right 的样式和颜色
            //worksheet.Cells.Style.Border.Top.Style = ExcelBorderStyle.Thin;
            //worksheet.Cells.Style.Border.Top.Color.SetColor(System.Drawing.Color.Black);

            //垂直居中
            worksheet.Cells.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

            //水平居中
            worksheet.Cells.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

            //单元格是否自动换行
            worksheet.Cells.Style.WrapText = false;

            //设置单元格格式为文本
            worksheet.Cells.Style.Numberformat.Format = "@";

            //单元格自动适应大小
            worksheet.Cells.Style.ShrinkToFit = true;


            ////第一种保存方式
            //string path1 = HttpContext.Current.Server.MapPath("Export/");
            //string filePath1 = path1 + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".xlsx";
            //FileStream fileStream1 = new FileStream(filePath1, FileMode.Create);
            ////保存至指定文件
            //FileInfo fileInfo = new FileInfo(filePath1);
            //package.SaveAs(fileInfo);

            ////第二种保存方式
            //string path2 = HttpContext.Current.Server.MapPath("Export/");
            //string filePath2 = path2 + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".xlsx";
            //FileStream fileStream2 = new FileStream(filePath2, FileMode.Create);
            ////写入文件流
            //package.SaveAs(fileStream2);


            //创建一个内存流,然后转换为字节数组,输出到浏览器下载
            //MemoryStream ms = new MemoryStream();
            //package.SaveAs(ms);
            //byte[] bytes = ms.ToArray();

            //也可以直接获取流
            //Stream stream = package.Stream;

            //也可以直接获取字节数组
            byte[] bytes = package.GetAsByteArray();

            //调用下面的方法输出到浏览器下载
            OutputClient(bytes);

            worksheet.Dispose();
            package.Dispose();
        }
public void ExportExcel(DataTable dt)
        {
            //新建一个 Excel 工作簿
            ExcelPackage package = new ExcelPackage();

            // 添加一个 sheet 表
            ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(dt.TableName);

            int rowIndex = 1;   // 起始行为 1
            int colIndex = 1;   // 起始列为 1

            //设置列名
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                worksheet.Cells[rowIndex, colIndex + i].Value = dt.Columns[i].ColumnName;

                //自动调整列宽,也可以指定最小宽度和最大宽度
                worksheet.Column(colIndex + i).AutoFit();
            }

            // 跳过第一列列名
            rowIndex++;

            //写入数据
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    worksheet.Cells[rowIndex + i, colIndex + j].Value = dt.Rows[i][j].ToString();
                }

                //自动调整行高
                worksheet.Row(rowIndex + i).CustomHeight = true;
            }

            //设置字体,也可以是中文,比如:宋体
            worksheet.Cells.Style.Font.Name = "Arial";

            //字体加粗
            worksheet.Cells.Style.Font.Bold = true;

            //字体大小
            worksheet.Cells.Style.Font.Size = 12;

            //字体颜色
            worksheet.Cells.Style.Font.Color.SetColor(System.Drawing.Color.Black);

            //单元格背景样式,要设置背景颜色必须先设置背景样式
            worksheet.Cells.Style.Fill.PatternType = ExcelFillStyle.Solid;
            //单元格背景颜色
            worksheet.Cells.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.DimGray);

            //设置单元格所有边框样式和颜色
            worksheet.Cells.Style.Border.BorderAround(ExcelBorderStyle.Thin, System.Drawing.ColorTranslator.FromHtml("#0097DD"));

            //单独设置单元格四边框 Top、Bottom、Left、Right 的样式和颜色
            //worksheet.Cells.Style.Border.Top.Style = ExcelBorderStyle.Thin;
            //worksheet.Cells.Style.Border.Top.Color.SetColor(System.Drawing.Color.Black);

            //垂直居中
            worksheet.Cells.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

            //水平居中
            worksheet.Cells.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

            //单元格是否自动换行
            worksheet.Cells.Style.WrapText = false;

            //设置单元格格式为文本
            worksheet.Cells.Style.Numberformat.Format = "@";

            //单元格自动适应大小
            worksheet.Cells.Style.ShrinkToFit = true;


            ////第一种保存方式
            //string path1 = HttpContext.Current.Server.MapPath("Export/");
            //string filePath1 = path1 + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".xlsx";
            //FileStream fileStream1 = new FileStream(filePath1, FileMode.Create);
            ////保存至指定文件
            //FileInfo fileInfo = new FileInfo(filePath1);
            //package.SaveAs(fileInfo);

            ////第二种保存方式
            //string path2 = HttpContext.Current.Server.MapPath("Export/");
            //string filePath2 = path2 + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".xlsx";
            //FileStream fileStream2 = new FileStream(filePath2, FileMode.Create);
            ////写入文件流
            //package.SaveAs(fileStream2);


            //创建一个内存流,然后转换为字节数组,输出到浏览器下载
            //MemoryStream ms = new MemoryStream();
            //package.SaveAs(ms);
            //byte[] bytes = ms.ToArray();

            //也可以直接获取流
            //Stream stream = package.Stream;

            //也可以直接获取字节数组
            byte[] bytes = package.GetAsByteArray();

            //调用下面的方法输出到浏览器下载
            OutputClient(bytes);

            worksheet.Dispose();
            package.Dispose();
        }

View Code

View Code

4858.com 634858.com 64

4858.com 654858.com 66

public void OutputClient(byte[] bytes)
        {
            HttpResponse response = HttpContext.Current.Response;

            response.Buffer = true;

            response.Clear();
            response.ClearHeaders();
            response.ClearContent();

            //response.ContentType = "application/ms-excel";
            response.ContentType = "application/vnd.openxmlformats - officedocument.spreadsheetml.sheet";
            response.AppendHeader("Content-Type", "text/html; charset=GB2312");
            response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx", DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")));

            response.Charset = "GB2312";
            response.ContentEncoding = Encoding.GetEncoding("GB2312");

            response.BinaryWrite(bytes);
            response.Flush();

            response.End();
        }
public void OutputClient(byte[] bytes)
        {
            HttpResponse response = HttpContext.Current.Response;

            response.Buffer = true;

            response.Clear();
            response.ClearHeaders();
            response.ClearContent();

            //response.ContentType = "application/ms-excel";
            response.ContentType = "application/vnd.openxmlformats - officedocument.spreadsheetml.sheet";
            response.AppendHeader("Content-Type", "text/html; charset=GB2312");
            response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx", DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")));

            response.Charset = "GB2312";
            response.ContentEncoding = Encoding.GetEncoding("GB2312");

            response.BinaryWrite(bytes);
            response.Flush();

            response.End();
        }

View Code

View Code

那种形式创制文件和出口到浏览器下载都以足以支撑 Excel .xlsx
格式的。越多相关属性设置,请参见:

那种方法创制文件和输出到浏览器下载都以足以支撑 Excel .xlsx
格式的。越来越多相关属性设置,请参见:

 

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图
Copyright @ 2010-2019 美高梅手机版4858 版权所有