|
Hi frinds,
First of all thanks, I got the code to Convert CSV file to XLS file using Excel Application object.
My code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using Microsoft.Office.Interop.Excel;
namespace File_Conversion
{
class Program
{
static void Main(string[] args)
{
ApplicationClass app = new ApplicationClass();
Workbook doc = app.Workbooks._Open(
@"C:\GCProject\Input Example.csv",
Missing.Value,
Missing.Value,
Missing.Value,
Missing.Value,
Missing.Value,
Missing.Value,
Missing.Value,
Missing.Value,
false,
Missing.Value,
Missing.Value,
false);
Console.WriteLine("Reading CSV File........");
doc.SaveAs(
@"C:\GCProject\Output.xls",
XlFileFormat.xlWorkbookNormal,
Missing.Value,
Missing.Value,
Missing.Value,
Missing.Value,
XlSaveAsAccessMode.xlExclusive,
Missing.Value,
false,
Missing.Value,
Missing.Value,
Missing.Value);
doc.Saved = true;
Console.WriteLine("Converted CSV to XLS file");
app.Quit();
Console.ReadLine();
}
}
}
But Now, I have to get specified format, Like follows.
My CSV file Contains Data like.
-----------------------------------
Ex: DriverID | CustomerID | Ship to ID | Load # | BOL Date/TimeStamp |PrdNam
520 | 2355 - | 1 | 61953 | ############ | 1 Eth 87
My XLS file Contains Data Like.
-----------------------------------------------------------------------------
Ex: Field | Definition | Length | Starting Column | Ending Column |
-----------------------------------------------------------------------------
delimiter"
-----------------------------------------------------------------------------
TransType|Some Definition |1 | 1 | 1
|
-----------------------------------------------------------------------------
delimiter ","
-----------------------------------------------------------------------------
Termin ID | Some Difinition| 3 | 3 | 5
|
-----------------------------------------------------------------------------
delimiter ","
-----------------------------------------------------------------------------
Term SPLC | Some Difi | 7 | 6 | 12
|
-----------------------------------------------------------------------------
I got the code converting CSV file to XLS file. But What I need is Converted
XLS file should look like above format in Converted Excel sheet.
Any body have Idea !!!!
Hope I explained well.!
Thanks in advance.
Regards,
Liyakhat.
|
|
|
|
|
here are 2 ideas:
1) do the open and save methods allow you to specify the format of the input/output? In the save parameter list there's XlFileFormat.xlWorkbookNormal - is there another format in that enumeration that better represents what you want? Try looking here[^] for information about those methods
2) parse the .csv and print your .xls files manually. This will obviously require more work but will ultimately give you much more flexibility with your format conversions. Here[^] is a short example of creating your own Excel file.
|
|
|
|
|
Hi Jammie,
First of all thanks for the reply.
I have tried with the "Open" and "Save" methods of the input/output. :(I have tried with the XlFileFormat.xlWorkbookNormal there is not methods to show in my specified format
I have read those two links which you have sent. But I m not able to find any info related to my task.
Do you have any new Idea ?
Thanks,
Liyakhat
|
|
|
|
|
The most versatile method is going to be to parse the .csv file and create a .xls file manually.
A .csv file is just a text file, so one method of parsing it is to open and read it with a StreamReader[^] and read it line by line. For each line that you read use the split[^] method to separate the values by comma.
Then to create your own .xls file just follow the example in the link that I gave here[^]. All you need to do is get a workbook like they do with this code:
oXL = new Excel.Application();
oXL.Visible = true;
oWB = (Excel._Workbook)(oXL.Workbooks.Add( Missing.Value ));
oSheet = (Excel._Worksheet)oWB.ActiveSheet;
and then write the data into the worksheet however you want. The cells in the .xls file can be accessed just like a two dimensional array, so you can arrange the data however you want. Then save the workbook and you're done!
Oh, and it's Jim, not Jammie
|
|
|
|
|
Hi Jim, not Jammie
I m very happy to tell you that I m able to start my task, You helped me lot in this. I m in half way to finish this task.
Provided links are very usefull to me.
Thanks,
Liyakhat
My Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
namespace ConsoleApplication_Sample
{
class Program
{
static void Main(string[] args)
ReadFromFile("C:\\GCProject\\Input Example_new.csv");
Excel.Application oXL;
Excel._Workbook oWB;
Excel._Worksheet oSheet;
Excel.Range oRng;
try
{
//Start Excel and get Application object.
oXL = new Excel.Application();
oXL.Visible = true;
//Get a new workbook.
oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
oSheet = (Excel._Worksheet)oWB.ActiveSheet;
//Add table headers going cell by cell.
oSheet.Cells[1, 1] = "Field";
oSheet.Cells[1, 2] = "Description";
oSheet.Cells[1, 3] = "Length";
oSheet.Cells[1, 4] = "Starting Column";
oSheet.Cells[1, 5] = "Ending Column";
//Format A1:D1 as bold, vertical alignment = center.
oSheet.get_Range("A1", "E1").Font.Bold = true;
//oSheet.get_Range("A1", "E1").AutoFormat(12, 1, 12, 10, 5, 0, 10);
oSheet.get_Range("A1", "E1").VerticalAlignment =
Excel.XlVAlign.xlVAlignCenter;
///*Start*/
StreamReader SR;
string S1;
SR = File.OpenText("C:\\GCProject\\Input Example_new.csv");
S1 = SR.ReadLine();
int i = 1;
while (S1 != null)
{
//Console.WriteLine(S);
string S;
S = S1;//SR.ReadLine();
if (S != null)
{
string[] spli = S.Split(new Char[] { ',' });
int j=2;
foreach (string s in spli)
{
if (s.Trim() != "")
{
oSheet.Cells[j, i] = s.ToString();//+ " Liya ";
Console.WriteLine(s);
}
j=j+1;
}
}
i = i + 1;
S1=SR.ReadLine();
}
SR.Close();
// /*End*/
//Fill A2:B6 with an array of values (First and Last Names).
// oSheet.get_Range("A2", "B24").Value2 = saNames;
oRng = oSheet.get_Range("A1", "E1");
oRng.EntireColumn.AutoFit();
oRng = oSheet.get_Range("B2", "B24");
oSheet.get_Range("b2", "b24").ColumnWidth = 75;
oRng.WrapText = true;
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
//AppendToFile();
}
// Reading the File
static void ReadFromFile(string filename)
{
StreamReader SR;
string S;
SR = File.OpenText(filename);
S = SR.ReadLine();
while (S != null)
{
Console.WriteLine(S);
S = SR.ReadLine();
if (S != null)
{
string[] spli = S.Split(new Char[] { ',' });
foreach (string s in spli)
{
}
}
}
SR.Close();
}
}
}
|
|
|
|
|
I am developing a program for pocket pc that uses sqlServer for the database. but when i start the program i have a message error that said that is missing sqlceme.dll.
The error appears when i want to use SqlCeEngine.
I can't add to Resurces and i copied it to Windows and in ProgramFiles to my application directory... but the i get the same error message...
What can i do? Please help me ...
Regards,
Marta Paniti.
--------------------------------
visit: http://pmartike.deviantart.com/
|
|
|
|
|
problem solved
--------------------------------
visit: http://pmartike.deviantart.com/
|
|
|
|
|
hi,
I am working on web application(ASP.NET ,VB.NET) and some reports push on server daily.
GZ (application like winzip) is installed on only server machine not on client machine.
I want to open report in GZ on server from any client machine and print on printer.
Can I do this ?
thanks in advance!!
|
|
|
|
|
No - you can not grab a file from the client, they have to send it to you. You could write a page which loads the report and starts printing, I believe, but at best it would bring up the print dialog, you can't start the printer from javascript.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Hello everyone,
I am learning how to use C# to implement an OLE DB provider (a simple one which is based on CSV file format). I can not find any samples from MSDN (all samples are C++).
Any reference materials?
thanks in advance,
George
|
|
|
|
|
Here is a quick google search[^]. Hope it helps you some
"Any sort of work in VB6 is bound to provide several WTF moments." - Christian Graus
|
|
|
|
|
Hi Paul,
Almost all on the list are about how to use OLE DB provider (as an OLE DB consumer), but what I am looking for is how to implement OLE DB provider, here is the related sample in C++,
http://msdn2.microsoft.com/en-us/library/ms715011.aspx
I am looking for a C# sample.
regards,
George
|
|
|
|
|
I must have misread your original post earlier. From a quick glance of msdn link, porting from C++ to C# shouldn't be that difficult.
"Any sort of work in VB6 is bound to provide several WTF moments." - Christian Graus
|
|
|
|
|
Hi Paul,
Do you know whether there are any tools to convert source codes from C++ to C#? The sample has almost 2k lines of code.
regards,
George
|
|
|
|
|
I don't really know of any tools for converting from C++ to C#. How much C# do you know? Are you learning it?
---modified
Here is one, I don't know how good it is...
http://code2code.net/[^]
---modified*2
Forget it, it couldn't even translate a simple Hello World prog...
"Any sort of work in VB6 is bound to provide several WTF moments." - Christian Graus
|
|
|
|
|
Thanks all the same.
regards,
George
|
|
|
|
|
i have in my application combobox that contain all logial drives into my pc
when i try to click drive a when no floppy in it error happen
-how to check if the selected drive is floopy or cd
-how to check if it has no files or folders in it
md_refay
|
|
|
|
|
You can't check if a has any files in it without trying to read it. If your file system reports the a drive, that's what will happen. However, it is possible to work out what is a USB drive, what's a floppy, a CD drive, or a hard drive. DriveInfo[] allDrives = DriveInfo.GetDrives (); Now if you iterate over those objects, each represents a drive, and what type of drive it is.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Can someone publish C# code to make use of CreateProcessWithTokenW function, so that i can run process just by getting token from current user, instead of passing user/pw for every run (WindowsIdentity.GetCurrent().Token)
Basically trying to extend impersonation functionality to disconnected functions like Process.Start()
Impersonation example:
http://www.codeproject.com/csharp/zetaimpersonator.asp
Many thanks.
|
|
|
|
|
You may want to use CreateProcessAsUser instead. This[^] link shows how to do it.
|
|
|
|
|
Thanks Pete, I need to use CreateProcessWithTokenW as to allow seemless impersonation; without having to manage credentials.
I found an interesting site, but that also does not have a sample code for me...http://www.pinvoke.net/[^]
|
|
|
|
|
I need help,
Like we set breakpoint in .net,
we can set condition and hit count of that breakpoint in Breakpoint window,
now I want to know how to set this condition?
Suppose there is variable x,
i want to set like if x=5,
then only it breaks or breakpoint hit,
can we do like this!!!!
plz explain in detail!!!!!
I tried on my own many times but either breakpoint does not hit, or it says condition is invalid!!!!!
I want to set a condition for variable in C#.net
|
|
|
|
|
Just set "i == 5" as the condition
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
hi frnd,
statement x=5 ...........set the value of x to 5.
it does not check whether x is equal to 5 or not.
You have to use conditional operator(==) instead of assignment(=) operator.
So use x==5 as a condition.
Ok.
Bye.
Nisar Inamdar.
|
|
|
|
|
hi all,
how to make a setup file in such a way that prerequisites are installed automatically before application is installed, e.g. if .net framework 2.0 is not in target machine it should check and installs it before installing the application.
thanks,
|
|
|
|
|