如何添加创建EXCEL表...从一个APLLICATION .. [英] HOW TO ADD CREATE EXCEL SHEET..FROM AN APLLICATION..

查看:65
本文介绍了如何添加创建EXCEL表...从一个APLLICATION ..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读取应用程序中每个像素的rgb值并将其存储在Excel工作表中。

当我选择文件并运行应用程序/ ..我收到一条消息..该应用程序正在屏幕的底部运行...但是实际的excel表填充了数据..即使我刷新它...它没有被加载(我的意思是大小不会改变..)...代码下面给出... plz chk

///一些代码

I am reading rgb values of each pixel in an application and storing it in an excel sheet.
when i select the file and run the application/..i get a message..that application is running at the bottom of screen...but actual excel sheet is filled with data..and even if i refresh it...it is not getting loaded(i mean the size does not change..)...the code is given below...plz chk
///some code

  using (FileStream fs = File.Create("DEST.XLS"))
                    {
                        using (TextWriter w = new StreamWriter(fs))
                        {
                            Bitmap bm = new Bitmap(textBox1.Text);
//textbox siginifes the file we are selcting for reading pixel data
                            for (i = 0; i < bm.Width; i++)
                            {
                                for (j = 0; j < bm.Height; j++)
                                {
                                    Color pixelColor = bm.GetPixel(i, j);
                                    b = pixelColor.R;
                                    w.Write(b + " ");
                                    c = pixelColor.G;
                                    w.Write(c + " ");
                                    d = pixelColor.B;
                                    w.Write(d + " ");
                                    w.Write("\r\n");
                                }
                            }
                            w.Close();
                        }
                    }





//DATA.XLS...GETS从我们选择图像的位置创建。



//我为EXCEL文件书写的格式正确 ..因为MSG COMES ..它不是正确的格式...但我可以打开它



//DATA.XLS...GETS CREATED FROM WHERE WE SELECTE THE IMAGE.

//IS THE FORMAT WHICH I HAVE WRITTEN FOR EXCEL FILE CORRECT..BECAUSE A MSG COMES..IT IS NOT IN CORRECT FORMAT...BUT I AM ABLE TO OPEN IT

推荐答案

我对您的代码进行了一些更改并测试了以下代码。它会创建一个逗号分隔值(CSV)文件,当您在文件资源管理器中双击 DEST.CSV 文件名时,该文件将被加载到Excel中。它适用于较小的图像文件。对于较大的图像文件,它会创建一个太大(太多行)的文件以供Excel加载。



我所做的更改:

1.将文件重命名为 DEST.CSV

2.在R和G值之间以及G和B值之间放置逗号

3.使用System.Diagnostics声明所有变量





I made a few changes to your code and tested the following code. It creates a comma-separated values (CSV) file that will be loaded into Excel when you double-click the DEST.CSV filename in File Explorer. It works fine for smaller image files. For larger image files, it creates a file that is too big (too many rows) for Excel to load.

Changes I made:
1. Renamed file to DEST.CSV
2. Put commas between R and G values and between G and B values
3. Declared all variables


using System.Diagnostics;

...

        private void button1_Click(object sender, EventArgs e)
        {
            using (FileStream fs = File.Create("DEST.CSV"))
            {
                using (TextWriter w = new StreamWriter(fs))
                {
                    int i;
                    int j;
                    int b;
                    int c;
                    int d
                    Bitmap bm = new Bitmap(textBox1.Text);
                    //textbox signifies the file we are selecting for reading pixel data
                    for (i = 0; i < bm.Width; i++)
                    {
                        for (j = 0; j < bm.Height; j++)
                        {
                            Color pixelColor = bm.GetPixel(i, j);
                            b = pixelColor.R;
                            w.Write(b + ",");
                            c = pixelColor.G;
                            w.Write(c + ",");
                            d = pixelColor.B;
                            w.Write(d);
                            w.Write("\r\n");
                            
                        }
                    }
                    w.Close();
                    //
                    // Load DEST.CSV into Excel
                    Process myProcess = new Process();
                    try
                    {
                        myProcess.StartInfo.UseShellExecute = true;
                        myProcess.StartInfo.FileName = "DEST.CSV";
                        myProcess.StartInfo.CreateNoWindow = true;
                        myProcess.Start();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }


这篇关于如何添加创建EXCEL表...从一个APLLICATION ..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆