Esempio n. 1
0
        /// <summary>
        /// ����
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                MSExcel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();

                try
                {
                    excelApp.Visible = false;
                    excelApp.DisplayAlerts = false;
                    /*excelApp.DisplayClipboardWindow = false;
                    // excelApp.DisplayDocumentActionTaskPane = false;
                    excelApp.DisplayDocumentInformationPanel = false;
                    excelApp.DisplayExcel4Menus = false;
                    excelApp.DisplayFormulaAutoComplete = false;
                    excelApp.DisplayFormulaBar = false;
                    excelApp.DisplayFullScreen = false;
                    excelApp.DisplayFunctionToolTips = false;
                    excelApp.DisplayInfoWindow = false;
                    excelApp.DisplayInsertOptions = false;
                    excelApp.DisplayNoteIndicator = false;
                    excelApp.DisplayPasteOptions = false;
                    excelApp.DisplayRecentFiles = false;
                    excelApp.Interactive = false;*/
                    excelApp.DisplayScrollBars = true;
                    excelApp.DisplayStatusBar = true;

                    MSExcel.Workbooks workBooks = excelApp.Workbooks;

                    // MSExcel.Workbook workbook = workBooks.Add(MSExcel.XlWBATemplate.xlWBATWorksheet);
                    MSExcel.Workbook workbook = (MSExcel.Workbook)workBooks.Open(Server.MapPath("DataTemplate.xlsx"),
                    Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                    Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                    Missing.Value, Missing.Value);

                    MSExcel.Worksheet worksheet = (MSExcel.Worksheet)excelApp.Worksheets.get_Item(1);

                    MSExcel.Range titleRange = worksheet.get_Range("1:1", Missing.Value);
                    titleRange.Font.Bold = true; // ����Ӵ�
                    titleRange.HorizontalAlignment = XlVAlign.xlVAlignCenter;

                    if (worksheet == null)
                    {
                        Console.WriteLine("ERROR in worksheet == null");
                    }

                    System.Collections.Generic.List<string> titles = new System.Collections.Generic.List<string>();
                    DataSet ds = new DataSet();
                    int curRow = 2;
                    string fileDir = Server.MapPath(@"\ResearchData\TmpFiles\");
                    string fileName = String.Empty;

                    #region �����

                    switch (DataType.SelectedValue)
                    {
                        case "1":
                            ExtendBLL.ResearchResult rr = new ExtendBLL.ResearchResult();
                            titles = rr.GetTitles();
                            ds = rr.GetAllList();

                            foreach (System.Data.DataRow row in ds.Tables[0].Rows)
                            {
                                worksheet.Cells[curRow, 1] = row["ID"];
                                worksheet.Cells[curRow, 2] = row["ChengGuoMingCheng"];
                                worksheet.Cells[curRow, 3] = row["ChengGuoLeiBie"];
                                worksheet.Cells[curRow, 4] = row["ChengGuoZuoZe"];
                                worksheet.Cells[curRow, 5] = row["DanWeiMingCheng"];
                                worksheet.Cells[curRow, 6] = row["BanZhengShiJian"];
                                worksheet.Cells[curRow, 7] = row["XueYuanShenHe"];
                                worksheet.Cells[curRow, 8] = row["XueXiaoShenHe"];
                                worksheet.Cells[curRow, 9] = row["ChengGuoDengJi"];
                                worksheet.Cells[curRow, 10] = row["ShuMingZuoZe"];
                                worksheet.Cells[curRow, 11] = row["ChengGuoBianHao"];
                                worksheet.Cells[curRow, 12] = row["ShiFouJiangLi"];
                                worksheet.Cells[curRow, 13] = row["ShuMingDanWei"];
                                worksheet.Cells[curRow, 14] = row["ChengGuoLieXing"];

                                curRow += 1;
                            }

                            fileName = "ResearchResult-" + DateTime.Now.ToString("yyyyMMddHHmmssff") + ".xlsx";

                            break;
                        case "2":
                            ExtendBLL.ResearchCost rc = new ExtendBLL.ResearchCost();
                            titles = rc.GetTitles();
                            ds = rc.GetAllList();

                            foreach (System.Data.DataRow row in ds.Tables[0].Rows)
                            {
                                worksheet.Cells[curRow, 1] = row["ID"];
                                worksheet.Cells[curRow, 2] = row["ChongJingZiHao"];
                                worksheet.Cells[curRow, 3] = row["XiangMuMingCheng"];
                                worksheet.Cells[curRow, 4] = row["XiangMuLieBie"];
                                worksheet.Cells[curRow, 5] = row["FuZeRen"];
                                worksheet.Cells[curRow, 6] = row["DanWeiMingCheng"];
                                worksheet.Cells[curRow, 7] = row["ShiDaoJingFei"];
                                worksheet.Cells[curRow, 8] = row["RuanJianJingFei"];
                                worksheet.Cells[curRow, 9] = row["GuangLiFeiBiLi"];
                                worksheet.Cells[curRow, 10] = row["GuangLiFei"];
                                worksheet.Cells[curRow, 11] = row["JingBanRen"];
                                worksheet.Cells[curRow, 12] = row["ShiDaoShiJian"];
                                worksheet.Cells[curRow, 13] = row["HuiChuYinHang"];

                                curRow += 1;
                            }

                            fileName = "ResearchCost-" + DateTime.Now.ToString("yyyyMMddHHmmssff") + ".xlsx";

                            break;
                        case "3":
                            ExtendBLL.ResearchArticle ra = new ExtendBLL.ResearchArticle();
                            titles = ra.GetTitles();
                            ds = ra.GetAllList();

                            foreach (System.Data.DataRow row in ds.Tables[0].Rows)
                            {
                                worksheet.Cells[curRow, 1] = row["ID"];
                                worksheet.Cells[curRow, 2] = row["LunWenTiMuZhongWen"];
                                worksheet.Cells[curRow, 3] = row["JiangLiZuoZe"];
                                worksheet.Cells[curRow, 4] = row["JiangLiZuoZeDanWei"];
                                worksheet.Cells[curRow, 5] = row["QiKanMingCheng"];
                                worksheet.Cells[curRow, 6] = row["QiKanNianJuan"];
                                worksheet.Cells[curRow, 7] = row["QiKanChuBanNianYue"];
                                worksheet.Cells[curRow, 8] = row["JianSuoHao"];
                                worksheet.Cells[curRow, 9] = row["XueYuanShenHe"];
                                worksheet.Cells[curRow, 10] = row["XueXiaoShenHe"];
                                worksheet.Cells[curRow, 11] = row["ShuRuRen"];
                                worksheet.Cells[curRow, 12] = row["ShuRuShiJian"];
                                worksheet.Cells[curRow, 13] = row["ShiFouRenLing"];
                                worksheet.Cells[curRow, 14] = row["LunWenShuMingZuoZe"];
                                worksheet.Cells[curRow, 15] = row["EIHeXinJianSuo"];
                                worksheet.Cells[curRow, 16] = row["CSCDKuoZhanQiKan"];
                                worksheet.Cells[curRow, 17] = row["QiKanShuXing"];
                                worksheet.Cells[curRow, 18] = row["QiKanGuoBie"];
                                worksheet.Cells[curRow, 19] = row["CSCDHeXinYuanQiKan"];
                                worksheet.Cells[curRow, 20] = row["SCIEJianSuo"];
                                worksheet.Cells[curRow, 21] = row["GaoShuiPingQiKan"];
                                worksheet.Cells[curRow, 22] = row["LunWenLeiBie"];
                                worksheet.Cells[curRow, 23] = row["CSSCIYuanQiKan"];
                                worksheet.Cells[curRow, 24] = row["EIKuoZhanYuanJianSuo"];

                                curRow += 1;
                            }

                            fileName = "ResearchArticle-" + DateTime.Now.ToString("yyyyMMddHHmmssff") + ".xlsx";

                            break;
                        case "4":
                        default:
                            ExtendBLL.ResearchProject rp = new ExtendBLL.ResearchProject();
                            titles = rp.GetTitles();
                            ds = rp.GetAllList();

                            foreach (System.Data.DataRow row in ds.Tables[0].Rows)
                            {
                                worksheet.Cells[curRow, 1] = row["ID"];
                                worksheet.Cells[curRow, 2] = row["XiangMuBianHao"];
                                worksheet.Cells[curRow, 3] = row["XiangMuPiZhunHao"];
                                worksheet.Cells[curRow, 4] = row["XiangMuMingCheng"];
                                worksheet.Cells[curRow, 5] = row["FuZeRen"];
                                worksheet.Cells[curRow, 6] = row["DanWeiMingCheng"];
                                worksheet.Cells[curRow, 7] = row["XiangMuLeiBie"];
                                worksheet.Cells[curRow, 8] = row["JingFeiBenHao"];
                                worksheet.Cells[curRow, 9] = row["HeTongJingFei"];
                                worksheet.Cells[curRow, 10] = row["GuoBoJingFei"];
                                worksheet.Cells[curRow, 11] = row["YiDaoJingFei"];
                                worksheet.Cells[curRow, 12] = row["RuanJianJingFei"];
                                worksheet.Cells[curRow, 13] = row["XiangMuKaiShiShiJian"];
                                worksheet.Cells[curRow, 14] = row["XiangMuJieShuShiJian"];
                                worksheet.Cells[curRow, 15] = row["GuangLiRen"];
                                worksheet.Cells[curRow, 16] = row["WeiTuoDanWei"];
                                worksheet.Cells[curRow, 17] = row["KeiTiZuChengYuan"];
                                worksheet.Cells[curRow, 18] = row["WeiTuoDanWeiGuangLiBuMen"];
                                worksheet.Cells[curRow, 19] = row["HeTongNianDu"];
                                worksheet.Cells[curRow, 20] = row["YiJiLeiBie"];

                                curRow += 1;
                            }

                            fileName = "ResearchProject-" + DateTime.Now.ToString("yyyyMMddHHmmssff") + ".xlsx";

                            break;
                    }

                    for (int i = 0; i < titles.Count; i++)
                    {
                        worksheet.Cells[1, i + 1] = titles[i];
                    }

                    #endregion

                    // ���ø�ʽ
                    MSExcel.Range allRange = worksheet.get_Range("A:Z", Missing.Value);
                    allRange.Columns.AutoFit();

                    // excelApp.Save(fileDir + fileName);
                    System.IO.FileInfo file = new System.IO.FileInfo(fileDir + fileName);

                    if (!System.IO.Directory.Exists(fileDir)) { System.IO.Directory.CreateDirectory(fileDir); }

                    // excelApp.Save(file.FullName);
                    workbook.SaveCopyAs(file.FullName);
                    // worksheet.SaveAs(fileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

                    Response.Clear();
                    Response.ContentEncoding = System.Text.Encoding.UTF8;
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(fileName));
                    Response.AddHeader("Content-Length", file.Length.ToString());
                    Response.ContentType = "application/ms-excel";

                    Response.WriteFile(file.FullName);

                    // file.Delete();
                }
                finally
                {
                    if (excelApp != null)
                    {
                        excelApp.Quit();
                    }
                }
            }
            catch (Exception ex)
            {
                WriteBackScript(string.Format("alert(\"{0}\")", Server.HtmlEncode(ex.Message)));
            }
        }