糖果派对官方网站_可以赌钱的糖果游戏_手机版
多少导出之--将GridView数据导出到Excel( 原卡塔尔

多少导出之--将GridView数据导出到Excel( 原卡塔尔

作者:网络编程    来源:未知    发布时间:2019-12-23 19:49    浏览量:

range.AttachDispatch(sheet.get_UsedRange());longiSheetCount=sheets.get_Count();range=range.get_Rows();longiRow=range.get_Row();//longiRow=range.get_Count();range=range.get_Columns();//longiColumn=range.get_Column();longiColumn=range.get_Count(卡塔尔(英语:State of Qatar);获取EXCEL的报表当前进和列的值有标题拿到行range=range.get_Rows();longiRow=range.get_Row();longiRow=range.get_Count(卡塔尔国;在报表无数据,七个得到都以1不会收获到0,有多少行数据,获取的都以1获取列longiColumn=range.get_Column(卡塔尔(قطر‎;从来拿到的都以1longiColumn=range.get_Count(卡塔尔(英语:State of Qatar);这一个得到的是健康的想问一下大佬,到底是何等难题,为何一向获得不到行

//将GridView导出到Excel

Hello,我们好!前几天把本人事前写的从数据库中导出Excel与我们方寸已乱下,有白玉微瑕还请指教!多谢!

 public static void DataGridToExcel(DataGridView dgv)       

效果描述:

{                      

透过传播差异的数量集来调节Excel中sheet的数量,此例依照两个为例,仅供我们参照他事他说加以考察:

if (dgv.Columns.Count == 0)           

#region 導出Excel
        private void outPutExcel(System.Data.DataTable dt, System.Data.DataTable dt2)
        {
            if (dt == null) return;
            Excel.Application xlApp = new Excel.Application();
            if (xlApp == null)
            {
                //clsLog.m_CreateErrorLog("不恐怕创制Excel对象,恐怕Computer未安装Excel", "", ""卡塔尔(قطر‎;
                return;
            }
            //創建Excel對象
            Excel.Workbooks workbooks = xlApp.Workbooks;
            Excel.Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
            //Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
            Excel.Worksheet worksheet = null;
            for (int sheetcount = 0; sheetcount < 2; sheetcount++卡塔尔国//循环依照本人索要的sheet的数额这里是多个
bb电子糖果派对,            {
                if (worksheet == null)
                {
                    worksheet = (Excel.Worksheet)workbook.Worksheets.Add(Type.Missing, Type.Missing, 1, Type.Missing);
                }
                else
                {
                    worksheet = (Excel.Worksheet)workbook.Worksheets.Add(Type.Missing, worksheet, 1, Type.Missing);
                }
                Excel.Range range = null;
                if (sheetcount == 0)
                {
                    long totalCount = dt.Rows.Count;
                    long rowRead = 0;
                    float percent = 0;
                    worksheet.Name = "明細資料生龙活虎";//第贰个sheet在Excel中展现的名称
                    ////写入题目
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        worksheet.Cells[1, i + 1] = dt.Columns[i].ColumnName;
                        range = (Excel.Range)worksheet.Cells[1, i + 1];
                        range.Interior.ColorIndex = 15;//背景颜色
                        range.Font.Bold = true;//粗体
                        range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;//居中
                        //加边框
                        range.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, null);
                        range.ColumnWidth = 4.63;//设置列宽
                        range.EntireColumn.AutoFit(卡塔尔(英语:State of Qatar);//自动调解列宽
                        //r1.EntireRow.AutoFit(卡塔尔(قطر‎;//自动调治行高
                    }

{  

                    //写入内容
                    for (int r = 0; r < dt.Rows.Count; r++)
                    {
                        for (int i = 0; i < dt.Columns.Count; i++)
                        {
                            worksheet.Cells[r + 2, i + 1] = dt.Rows[r][i];
                            range = (Excel.Range)worksheet.Cells[r + 2, i + 1];
                            range.Font.Size = 9;//字体大小
                            //加边框
                            range.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, null);
                            range.EntireColumn.AutoFit(卡塔尔;//自动调度列宽
                        }
                        rowRead++;
                        percent = ((float)(100 * rowRead)) / totalCount;
                    }
                    range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight = Excel.XlBorderWeight.xlThin;
                    if (dt.Columns.Count > 1)
                    {
                        range.Borders[Excel.XlBordersIndex.xlInsideVertical].Weight = Excel.XlBorderWeight.xlThin;
                    }
                }
                else if (sheetcount == 1)
                {
                    long totalCount = dt2.Rows.Count;
                    long rowRead = 0;
                    float percent = 0;
                    worksheet.Name = "建議解決方案";
                    ////写入标题
                    for (int i = 0; i < dt2.Columns.Count; i++)
                    {
                        worksheet.Cells[1, i + 1] = dt2.Columns[i].ColumnName;
                        range = (Excel.Range)worksheet.Cells[1, i + 1];
                        range.Interior.ColorIndex = 15;//背景颜色
                        range.Font.Bold = true;//粗体
                        range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;//居中
                        //加边框
                        range.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, null);
                        range.ColumnWidth = 14.63;//设置列宽
                        range.EntireColumn.AutoFit(卡塔尔(英语:State of Qatar);//自动调治列宽
                        //r1.EntireRow.AutoFit(卡塔尔(英语:State of Qatar);//自动调治行高
                    }
                    //写入内容
                    for (int r = 0; r < dt2.Rows.Count; r++)
                    {
                        for (int i = 0; i < dt2.Columns.Count; i++)
                        {
                            worksheet.Cells[r + 2, i + 1] = dt2.Rows[r][i];
                            range = (Excel.Range)worksheet.Cells[r + 2, i + 1];
                            range.Font.Size = 9;//字体大小
                            //加边框
                            range.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, null);
                            range.EntireColumn.AutoFit(卡塔尔(قطر‎;//自动调节列宽
                        }
                        rowRead++;
                        percent = ((float)(100 * rowRead)) / totalCount;
                        //System.Windows.Forms.Application.DoEvents();
                    }
                    range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight = Excel.XlBorderWeight.xlThin;
                    if (dt2.Columns.Count > 1)
                    {
                        range.Borders[Excel.XlBordersIndex.xlInsideVertical].Weight = Excel.XlBorderWeight.xlThin;
                    }
                }
            }

return;           

//上面是将Excel存款和储蓄在服务器上钦定的路径与存款和储蓄的称谓
            try
            {
                string tPath = System.AppDomain.CurrentDomain.BaseDirectory;
                if (!Directory.Exists(tPath + "Excel"))
                {
                    Directory.CreateDirectory(tPath + "Excel");
                }
                workbook.SaveCopyAs(tPath + "Excel" + "\" + System.DateTime.Today.ToString("yyyyMMdd") + pCompany + "测试.xls");
                }
                //writeLog("生成Exce附属类小构件成功!"卡塔尔(قطر‎;//此处是自家的三个艺术,记录log新闻的
            }
            catch(Exception ex)
            {
                string strMsgErr = "生成Excel附属类小构件過程中出現異常,詳細音信如:" + ex.ToString(卡塔尔;
                //writeLog(strMsgErr卡塔尔;//这些也是写log档的
            }
            //大家导出Excel的时候会在经过中调用Excel进度,导出之后必需强制杀掉进度            
            try
            {
                if (xlApp != null)
                {
                    int lpdwProcessId;
                    GetWindowThreadProcessId(new IntPtr(xlApp.Hwnd), out lpdwProcessId);
                    System.Diagnostics.Process.GetProcessById(lpdwProcessId).Kill();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Delete Excel Process Error:" + ex.Message);
            } 
        }
        #endregion

}           

到此地从数据库中程导弹出Excel就完事了,并生成在对应的目录下。

Excel.Application exc = new Excel.ApplicationClass();           

那只是二个轻松易行的导出方法,入眼是大家要学会通过那么些法子,将其选用到别的的导出Excel的功能供给中,那个时候大家要求做的或是有转移参数字传送入的个数,类型,等等。

if (exc == null)           

今后小编还恐怕会恭喜出,动态的从数据库中程导弹出Excel,并将其看成发送邮件的附属类小零件发送到钦定的收受人,以至将发送的邮件实现的保存成二进制文件可能XML文件,供我们搜索,假若发送退步以来,无需重新生成全部的东西,只需调用保存的二进制文件大概XML文书档案就能够实现重新发送邮件,方便火速!

{               

OK啦,后日就先说那样多啊!希望有更加好的落真实情况势的对象能够恢复生机小编,将你的素材分享出来!后会有期!

throw new Exception("Excel不能够运维"卡塔尔国;           

}           

ProcessQueryingForm pProcessfrm = new ProcessQueryingForm();           

pProcessfrm.SetProcessText("正在展开Excel,请稍候....");           

pProcessfrm.Show();           

System.Windows.Forms.Application.DoEvents(卡塔尔(قطر‎;//'转让调节权,未有这一句的话提醒窗口不能够健康展现

Excel.Workbooks workbooks = exc.Workbooks;           

Excel._Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);

Excel.Sheets sheets = exc.Sheets;           

Excel._Worksheet worksheet = (Excel._Worksheet)sheets[1];           

if (worksheet == null)           

{               

throw new Exception("Excel运营错误"卡塔尔国;           

下一篇:没有了
友情链接: 网站地图
Copyright © 2015-2019 http://www.tk-web.com. bb电子糖果派对有限公司 版权所有