使用Visual Studio 2010并具有Excel报表生成器

六月猫

我有很长的数字序列(例如10000203005595000),当我将值传递给excel时,我有一个(例如10000E ^ + 10)值,C#中的代码应该是什么来操纵格式单元格/数字/类别/分数。要具有特定的价值,我有。请帮助我,谢谢:-)

这是我的程序:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using Microsoft.Office.Interop.Excel;
using System.Text.RegularExpressions;

namespace BIS
{
    public partial class labDB : Form
    {
        System.Data.DataTable data;

        public labDB()
        {
            InitializeComponent();
        } 
        //this is my report generator using Excel
        private void genReport_Click(object sender, EventArgs e)
        {
            saveFileDialog1.InitialDirectory = "C:";
            saveFileDialog1.Title = "Save as Excel File";
            saveFileDialog1.FileName = "Laboratory Department Inventory Report";
            saveFileDialog1.Filter = "Excel Files(2003)|*.xls|Excel Files(2007)|*.xlsx|Excel Files(2010)|*.xlsx|Excel Files(2013)|*.xlsx";
            if (saveFileDialog1.ShowDialog() != DialogResult.Cancel)
            {
                Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
                ExcelApp.Application.Workbooks.Add(Type.Missing);

                ExcelApp.Columns.ColumnWidth = 25;

                for (int i = 1; i < dataGridViewLAB.Columns.Count + 1; i++)
                {
                    ExcelApp.Cells[1, i] = dataGridViewLAB.Columns[i - 1].HeaderText;
                }
                for (int i = 0; i < dataGridViewLAB.Rows.Count; i++)
                {
                    for (int j = 0; j < dataGridViewLAB.Columns.Count; j++)
                    {
                        ExcelApp.Cells[i + 2, j + 1] = dataGridViewLAB.Rows[i].Cells[j].Value.ToString();
                    }
                }
                ExcelApp.ActiveWorkbook.SaveCopyAs(saveFileDialog1.FileName.ToString());
                ExcelApp.ActiveWorkbook.Saved = true;
                ExcelApp.Quit();
            }
        }
    }
}
希巴
             using System;  
             using System.Collections.Generic;   
             using System.Data;  
             using System.Linq;  
             using System.Text;  
             using System.Threading.Tasks;  
             using System.Windows;  
             using System.Windows.Forms;  
             using Office = Microsoft.Office.Core;  
             using Excel = Microsoft.Office.Interop.Excel;

             namespace Excel   
             {   
                 public class ExcelUtlity   
                 {                      

    public bool WriteDataTableToExcel(System.Data.DataTable dataTable, string worksheetName, string saveAsLocation, string ReporType)        {
        Microsoft.Office.Interop.Excel.Application excel;
        Microsoft.Office.Interop.Excel.Workbook excelworkBook;
        Microsoft.Office.Interop.Excel.Worksheet excelSheet;
        Microsoft.Office.Interop.Excel.Range excelCellrange;
        try
        {
            // Start Excel and get Application object.
           excel = new Microsoft.Office.Interop.Excel.Application();
            // for making Excel visible
            excel.Visible = false;
            excel.DisplayAlerts = false;
            // Creation a new Workbook
            excelworkBook = excel.Workbooks.Add(Type.Missing);
            // Workk sheet             
            excelSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelworkBook.ActiveSheet;
            excelSheet.Name = worksheetName;
            excelSheet.Cells[1, 1] = ReporType;
            excelSheet.Cells[1, 2] = "Date : " + DateTime.Now.ToShortDateString();               
            // loop through each row and add values to our sheet
            int rowcount = 2;
            foreach (DataRow datarow in dataTable.Rows)
            {
                rowcount += 1;
                for (int i = 1; i <= dataTable.Columns.Count; i++)
                {
                    // on the first iteration we add the column headers
                    if (rowcount == 3)
                    {
                        excelSheet.Cells[2, i] = dataTable.Columns[i-1].ColumnName;
                        excelSheet.Cells.Font.Color = System.Drawing.Color.Black;
                    }
                    excelSheet.Cells[rowcount, i] = datarow[i-1].ToString();
                    //for alternate rows
                    if (rowcount > 3)
                    {
                        if (i == dataTable.Columns.Count)
                        {
                            if (rowcount % 2 == 0)
                            {
                                excelCellrange = excelSheet.Range[excelSheet.Cells[rowcount, 1], excelSheet.Cells[rowcount, dataTable.Columns.Count]];
                                FormattingExcelCells(excelCellrange, "#CCCCFF", System.Drawing.Color.Black,false);
                            }
                        }
                    }
                }
            }
            // now we resize the columns
            excelCellrange = excelSheet.Range[excelSheet.Cells[1, 1], excelSheet.Cells[rowcount, dataTable.Columns.Count]];
            excelCellrange.EntireColumn.AutoFit();
            Microsoft.Office.Interop.Excel.Borders border = excelCellrange.Borders;
            border.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
            border.Weight = 2d;
            excelCellrange = excelSheet.Range[excelSheet.Cells[1, 1], excelSheet.Cells[2, dataTable.Columns.Count]];
            FormattingExcelCells(excelCellrange, "#000099", System.Drawing.Color.White, true);
            //now save the workbook and exit Excel
            excelworkBook.SaveAs(saveAsLocation);;
            excelworkBook.Close();
            excel.Quit();
            return true;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            return false;
        }
        finally
        {
            excelSheet = null;
            excelCellrange = null;
            excelworkBook = null;
        }
    }       
    /// FUNCTION FOR FORMATTING EXCEL CELLS

    public void FormattingExcelCells(Microsoft.Office.Interop.Excel.Range range, string HTMLcolorCode, System.Drawing.Color fontColor, bool IsFontbool)
    {
        range.Interior.Color = System.Drawing.ColorTranslator.FromHtml(HTMLcolorCode);
        range.Font.Color = System.Drawing.ColorTranslator.ToOle(fontColor);
        if (IsFontbool == true)
        {
            range.Font.Bold = IsFontbool;
        }
    }
}

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Visual Studio 2013中打开报表生成器报表

Visual Studio 2012查询生成器

在Visual Studio代码中是否有带有Ominpascal扩展名的GUID生成器

适用于Visual Studio的Microsoft SQL Server特定的解析器和脚本生成器> 2010?

使用 Visual Studio 2010 重建

什么是Visual Studio 2019的cmake生成器

使用具有大量数据的 python 生成器

CMake无法找到Visual Studio 15 2017生成器的实例,但可以与Visual Studio 16 2019一起正常使用

如何将TypeScript 1.6与Visual Studio Code一起使用以获得生成器支持?

如何使用Visual Studio for iOS生成IPA

如何在Visual Studio 2010中使用DEF文件从现有C ++代码生成DLL

Visual Studio中的单元测试生成器不起作用

单个文件生成器不适用于Visual Studio 2017中的.NET Standard项目

如何阻止Visual Studio查询生成器重写SQL(VS2005)

Sandcastle 帮助文件生成器项目未显示在 Visual Studio 2017 Enterprise 中

如何在Visual Studio 2013 Express中安装单元测试生成器扩展

如何在CMake中为Visual Studio生成器添加预编译头

在没有Excel的PC上使用Excel的Visual Studio程序

使用 Microsoft 报表生成器:当没有数据时,如何添加“无数据可用”消息?

使用Asm字节码生成器(ClassWriter)生成具有通用类型的方法

Visual Studio 2013:在报表向导中打开现有报表?

如何使Visual Studio 2010警告未使用的变量?

无法在Visual Studio 2010中使用CSS3

将TFS 2013与Visual Studio 2010结合使用

使用Visual Studio 2010资源编辑器对齐控件

如何在Visual Studio 2010中使用Boost

使用 Visual Studio 2010 进行 std::array 初始化

使用Visual Studio 2010进行内存分析

在Visual Studio 2010上使用数据集进行RDLC报告