ImageVerifierCode 换一换
格式:DOCX , 页数:16 ,大小:18.15KB ,
资源ID:7772852      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bingdoc.com/d-7772852.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Excel文件的导入导出文档格式.docx)为本站会员(b****4)主动上传,冰点文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰点文库(发送邮件至service@bingdoc.com或直接QQ联系客服),我们立即给予删除!

Excel文件的导入导出文档格式.docx

1、 / 读取Excel文件转换成DataTableparam name=excelPathExcel文件路径sheetName/returns public static System.Data.DataTable Import_Sheet(string excelPath, string sheetName) return Import_Sql(excelPath, select * from + sheetName + $); sqlExcel内查询语句 public static System.Data.DataTable Import_Sql(string excelPath, stri

2、ng sql) /string 2003 = Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;data source= + filepath; /string 2007 = Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;HDR=YES; + fPath;/读EXCEL2003/excel2007 OleDbConnection conn = new OleDbConnection(Excel 8.0;HDR=YES;HDR=1; +

3、excelPath); OleDbDataAdapter odda = new OleDbDataAdapter(sql, conn); System.Data.DataTable dt = new System.Data.DataTable(); try conn.Open(); odda.Fill(dt); catch (Exception ex) throw ex; finally conn.Close(); conn.Dispose(); odda.Dispose(); return dt; #endregion #region 输出Excel文件 #region 使用office组件

4、输出 public static void Export_Office(System.Data.DataTable dt) Export_Office(dt, , new Model.harry.ExcelInfo(); public static void Export_Office(System.Data.DataTable dt, Model.harry.ExcelInfo model) Export_Office(dt, , model); public static void Export_Office(System.Data.DataTable dt, string savePat

5、h) Export_Office(dt, savePath, new Model.harry.ExcelInfo(); / 输出Excel文件dt数据源savePath储存路径ExcelInfoExcel格式化信息对象 0) Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel._Worksheet format = (Microsoft.Office.Interop.Excel._Wo

6、rksheet)excel.Application.Workbooks.Add(true).ActiveSheet; /设置行和列的索引 int rowIndex = 1, colIndex = 0; /添加列名 foreach (DataColumn col in dt.Columns) colIndex+; excel.Cells1, colIndex = col.ColumnName; /添加数据 foreach (DataRow row in dt.Rows) rowIndex+; colIndex = 0; excel.CellsrowIndex, colIndex = rowcol

7、.ColumnName.ToString(); if (model != null) /设置标题是否为粗体 format.get_Range(excel.Cells1, 1, excel.Cells1, colIndex).Font.Bold = model.TitleBold; Range range = format.get_Range(excel.Cells1, 1, excel.CellsrowIndex, colIndex); /设置字体大小 range.Font.Size = model.FontSize; /设置列宽 if (model.Width = Model.harry.E

8、xcelInfo.WidthType.auto) range.Columns.AutoFit(); else if (model.Width = Model.harry.ExcelInfo.WidthType.size) range.ColumnWidth = model.WidthSize; /设置对齐格式 if (model.Align = Model.harry.ExcelInfo.AlignType.left) range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft; else i

9、f (model.Align = Model.harry.ExcelInfo.AlignType.center) range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; else if (model.Align = Model.harry.ExcelInfo.AlignType.right) range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignRight; else if (model

10、.Valign = Model.harry.ExcelInfo.ValignType.top) range.VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignTop; else if (model.Valign = Model.harry.ExcelInfo.ValignType.middle) range.VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter; else if (model.Valign = Mo

11、del.harry.ExcelInfo.ValignType.bottom) range.VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignBottom; /不可见,即后台处理 excel.Visible = false; /设置禁止弹出保存的询问提示框 excel.DisplayAlerts = false; /设置禁止弹出覆盖的询问提示框 /excel.AlertBeforeOverwriting = false; if (savePath != ) excel.ActiveWorkbook.SaveCo

12、pyAs(savePath); else excel.Save(AppDomain.CurrentDomain.BaseDirectory + bak.xls if (System.IO.File.Exists(AppDomain.CurrentDomain.BaseDirectory + ) System.IO.File.Delete(AppDomain.CurrentDomain.BaseDirectory + catch excel.Application.Workbooks.Close(); excel.Application.Quit(); excel.Quit(); /释放使用的E

13、xcel对象 System.Runtime.InteropServices.Marshal.ReleaseComObject(excel); GC.Collect(); KillExcel(); /删除执行过程中未释放的Excel线程 private static void KillExcel() System.Diagnostics.Process excelProcesses = System.Diagnostics.Process.GetProcessesByName(EXCEL DateTime startTime = new DateTime(); int processId = 0

14、; for (int i = 0; i excelProcesses.Length; i+) if (startTime excelProcessesi.StartTime) startTime = excelProcessesi.StartTime; processId = i; if (excelProcessesprocessId.HasExited = false) excelProcessesprocessId.Kill(); #region 使用DataGrid输出 / 使用DataGrid输出Excel文件DataGridDataGrid public static void E

15、xport_DataGrid(DataGrid dg) Export_DataGrid(dg, null, null, null, false);fileName文件名称 public static void Export_DataGrid(DataGrid dg, string fileName) Export_DataGrid(dg, null, fileName, null, false);formatCellIndex需要格式化的列集合,例如$1$2$为1,2列格式化为字符串 public static void Export_DataGrid(DataGrid dg, string

16、fileName, string formatCellIndex) Export_DataGrid(dg, null, fileName, formatCellIndex, false);isWrite是否直接输出到前台Excel文件内容 public static string Export_DataGrid(DataGrid dg, string fileName, string formatCellIndex, bool isWrite) return Export_DataGrid(dg, null, fileName, formatCellIndex, isWrite);DataTa

17、bleDataTable数据源 public static void Export_DataGrid(System.Data.DataTable dt) Export_DataGrid(null, dt, null, null, false); public static void Export_DataGrid(System.Data.DataTable dt, string fileName) Export_DataGrid(null, dt, fileName, null, false); public static void Export_DataGrid(System.Data.Da

18、taTable dt, string fileName, string formatCellIndex) Export_DataGrid(null, dt, fileName, formatCellIndex, false); public static string Export_DataGrid(System.Data.DataTable dt, string fileName, string formatCellIndex, bool isWrite) return Export_DataGrid(null, dt, fileName, formatCellIndex, isWrite)

19、; private static string Export_DataGrid(DataGrid dg, System.Data.DataTable dt, string fileName, string formatCellIndex, bool isWrite) if (dg = null) dg = new DataGrid(); dg.DataSource = dt; dg.DataBind(); foreach (DataGridItem dgi in dg.Items) for (int j = 0; j dgi.Cells.Count; j+) if (formatCellInd

20、ex != null & formatCellIndex ! & formatCellIndex.Contains($ + j + ) dgi.Cellsj.Attributes.Add(style, mso-number-format: if (fileName = null | fileName = ) fileName = Excel.xls System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); dg.RenderControl(new System.Web.UI.HtmlTextWriter(oStri

21、ngWriter); if (isWrite) return oStringWriter.ToString(); /this.EnableViewState = false; HttpResponse response = System.Web.HttpContext.Current.Response; response.Clear(); response.Buffer = true; response.ContentType = application/vnd.ms-excel response.Charset = gb2312 response.ContentEncoding = Syst

22、em.Text.Encoding.GetEncoding( response.AppendHeader(content-dispositionattachment;fileName= + fileName + response.Write(oStringWriter.ToString(); response.End(); return #region 使用GridView输出 / 使用GridView输出Excel文件GridViewGridView public static void Export_GridView(GridView gv) Export_GridView(gv, null, null, null, false);s

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2