具体一点,生成excel的代码是什么?

解决方案 »

  1.   

    代码如下:
     Excel.Application ExcelApp = new Excel.Application();//Excel应用程序实例
                ExcelApp.DisplayAlerts = false;
                try
                {
                    string TemplateFileName = string.Empty;
                    if(flag==0)
                    TemplateFileName = GetApplicationPath() + "\\NewReportTemplate\\AllUnit.xls";
                    else if(flag==1)
                    TemplateFileName = GetApplicationPath() + "\\NewReportTemplate\\CompleteRate.xls";                ExcelApp.Workbooks.Open(TemplateFileName, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss);
                    Excel.Workbooks ExcelBooks = (Excel.Workbooks)ExcelApp.Workbooks; ;//工作表集合 
                    Excel.Workbook ExcelBook = ExcelApp.ActiveWorkbook;//当前操作的工作表 
                    Excel.Worksheet sheet = (Excel.Worksheet)ExcelBook.ActiveSheet;//当前操作的表格 
                   if(flag==0)
                    sheet.Cells[2, 1] = string.Format("月份:{0}年{1}月", HeaderList["YEAR"], HeaderList["MONTH"]);
                   else if(flag==1)
                    sheet.Cells[2, 1] = HeaderList["STATENDDATE"];
                   // sheet.Cells[ExcelReportByMonth.Properties.Settings.Default.PrivRowCount, 1] = "报表生成时间:" + DateTime.Now.ToString("yyyy年MM月dd日");
                   // sheet.Cells[ExcelReportByMonth.Properties.Settings.Default.PrivRowCount - 1, 1] = "说明:空白表示数据无效、“/”表示无需计算、斜体字表示数据可疑、正体字表示数据正常";
                    int RowCount = dsReport.Tables[0].Rows.Count;
                    int ColumnCount = dsReport.Tables[0].Columns.Count;
                    SortedList<string, List<int>> MergeCellsCollection = new SortedList<string, List<int>>();
                    for (int i = 5; i < RowCount + 5; i++)
                    {
                        DataRow dr = dsReport.Tables[0].Rows[i - 5];
                        for (int j = 1; j < ColumnCount; j++)
                        {
                            if (dr[j - 1].ToString().IndexOf("online") != -1)
                            {
                                Excel.Range RangeTech = sheet.get_Range(sheet.Cells[i, j], sheet.Cells[i, j]);
                                RangeTech.Font.Bold = true;
                                RangeTech.Font.Italic = true;
                            }
                            sheet.Cells[i, j] = dr[j - 1].ToString().Replace("online", "").Replace("A", "").Replace("B", "").Replace("C", "");
                            // sheet.Cells[i, j] = dr[j - 1].ToString();
                        }
                        //以下代码整理那些行需要合并,真TMD麻烦。
                        string PlantName = dr[1].ToString();    //第一列为电厂名称列
                        if (MergeCellsCollection.IndexOfKey(PlantName) == -1)
                        {
                            List<int> MergeCellList = new List<int>();
                            MergeCellList.Add(i);
                            MergeCellsCollection.Add(PlantName, MergeCellList);
                        }
                        else
                        {
                            MergeCellsCollection[PlantName].Add(i);
                        }
                    }                for (int k = 0; k < MergeCellsCollection.Count; k++)
                    {
                        int StartRow = MergeCellsCollection.Values[k][0];
                        int EndRow = StartRow + MergeCellsCollection.Values[k].Count - 1;
                        for (int m = StartRow; m < EndRow + 1; m++)
                        {
                            sheet.Cells[m, 2] = "";
                        }
                        Excel.Range range = sheet.get_Range(ExcelApp.Cells[StartRow, 2], ExcelApp.Cells[EndRow, 2]);//获得用到的第一行的范围
                        range.Merge(true);//合并
                        range.MergeCells = true;//合并单元格
                        range.Value = MergeCellsCollection.Keys[k];
                    }
                    Excel.Range RangeTechnics;
                    //绘制框架
                    RangeTechnics = sheet.get_Range(sheet.Cells[5, 1], sheet.Cells[RowCount + 4, ColumnNum]);
                    RangeTechnics.Borders.LineStyle = 1;
                    RangeTechnics.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
                    RangeTechnics.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
                    RangeTechnics.Font.Size = 9;
                    //报表日期
                    RangeTechnics = sheet.get_Range(sheet.Cells[RowCount + 5, 1], sheet.Cells[RowCount + 5, ColumnNum]);
                    RangeTechnics.Value2 = "报表时间:" + DateTime.Today.ToString("yyyy年MM月dd日");
                    RangeTechnics.HorizontalAlignment = Excel.XlHAlign.xlHAlignRight;
                    RangeTechnics.VerticalAlignment = Excel.XlVAlign.xlVAlignBottom;
                    RangeTechnics.Font.Italic = true;
                    RangeTechnics.Font.Size = 9;
                    RangeTechnics.MergeCells = true;               
                    #region 保存文件,退出操作
                    string DirectoryName = string.Empty;
                    if(flag==0)
                    DirectoryName = HeaderList["CREATEREPORTPATH"] + "Report\\AllUnitMonthReport\\";
                    else if(flag==1)
                    DirectoryName = HeaderList["CREATEREPORTPATH"] + "Report\\CompleteRateMonthReport\\";                if (System.IO.Directory.Exists(DirectoryName) == false)
                    {
                        System.IO.Directory.CreateDirectory(DirectoryName);
                    }
                    string FileName = string.Format(DirectoryName + string.Format("{0}.{1}.xls", HeaderList["YEAR"], HeaderList["MONTH"]));
                    if (System.IO.File.Exists(FileName))
                    {
                        System.IO.File.Delete(FileName);
                    }
                    sheet.SaveAs(FileName, miss, miss, miss, miss, miss, Excel.XlSaveAsAccessMode.xlNoChange, Excel.XlSaveConflictResolution.xlLocalSessionChanges, miss);
                    #endregion
                }
                finally
                {
                    ExcelApp.Quit();
                }
      

  2.   

    就是用C#做个报表生成工具,把一些GrieView中的数据生成EXcel报表(其中用到了Excel模板),且电厂名称相同的要合并。当报表生成后,预览发现合并的行在分页的时候出现断行现象。和Word中出现的现象是一样的,但Word中设置一下属性就可以了,但Excel中没有找到相应的属性设置。那位大虾知道吗