save a excel file as a tab delimited text file

S

shantanu

Hi all
i am trying to save a excel file as a tab delimited text file. can i
do it some how?

i using the code

_sheet1.SaveAs(@"C:\PRP
\PRPTemp.txt",Excel.XlFileFormat.xlTextWindows ,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);

Kindly assist
regards
shantanu
 
M

Mark Rae

i am trying to save a excel file as a tab delimited text file. can i
do it some how?

i using the code

_sheet1.SaveAs(@"C:\PRP
\PRPTemp.txt",Excel.XlFileFormat.xlTextWindows
,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);

Please show your full code...

It looks like you are trying to use server-side Office Automation, which
won't work:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2
 
S

shantanu

Please show your full code...

It looks like you are trying to use server-side Office Automation, which
won't work:http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2

--http://www.markrae.net

hi Thanks
here is the code

#region "Open Development Plan Sheet"
private void OpenDPS()
{
try
{
int i;
string[] arrPlanned;
string[] arrMilestone;

int cellloc=0;

Excel.Range testRng, test;
Excel.Range XLSrange,xlsRngPlanned,xlsRngMilestone;
Excel.Range ProjNo,ProjNm;


_book = OpenExcelWorkbook(strXlsFile);
_sheet = (Excel.Worksheet)_book.ActiveSheet;
_sheet.Select(Type.Missing);

Excel.Worksheet _shtOut = (Excel.Worksheet)_book.Worksheets[1];


while(_shtOut.Name != "DevelopmentPlan")
{
_shtOut = (Excel.Worksheet)_shtOut.Next;
}

XLSrange = _shtOut.UsedRange;

ReadPRPTemp();

arrPlanned = new string[arrHeader.Length];

for (i = 16 ; i <= XLSrange.Rows.Count ; i++)
{
xlsRngPlanned = _shtOut.get_Range("C" +
i.ToString(),Type.Missing);


if(xlsRngPlanned.Value!=null)
{
arrPlanned= xlsRngPlanned.Value.ToString();
}
else
{
arrPlanned = "";
}
}

arrMilestone = new string[arrHeader.Length];

TextWriter t = new StreamWriter(@"C:\PRP\PRPTemp.xls");
foreach(string s in arrHeader)
{
t.Write(s.ToUpper() + "\t");
}
t.Close();

for (int l = 16 ; l <= XLSrange.Rows.Count-1 ; l++)
{
xlsRngMilestone = _shtOut.get_Range("B" +
l.ToString(),Type.Missing);


if(xlsRngMilestone.Value!=null)
{
arrMilestone[l] = xlsRngMilestone.Value.ToString() +
".PLANNED";
}
else
{
break;
}
}
ProjNo = _shtOut.get_Range("H6",Type.Missing);
ProjNm = _shtOut.get_Range("H7",Type.Missing);

_book1 = OpenExcelWorkbook(@"C:\PRP\PRPTemp.xls");
_sheet1 = (Excel.Worksheet)_book1.ActiveSheet;
_sheet1.Select(Type.Missing);

TextWriter twr = new StreamWriter(@"C:\WINDOWS\Temp
\PRPLogin.txt");
twr.WriteLine("1");
twr.Close();

Excel.Worksheet _shtOut1 = (Excel.Worksheet)_book1.Worksheets[1];
testRng = _shtOut1.UsedRange;

_shtOut1.get_Range("A2",Type.Missing).Value = ProjNo.Value;
_shtOut1.get_Range("B2",Type.Missing).Value = ProjNm.Value;

//MessageBox.Show(testRng.Columns.h);


for(int x = 1;x<= testRng.rows.Count; x++)
{
test = _shtOut1.get_Range("A" + x.ToString(),Type.Missing);
if(test.Value!=null)
{
for (int l = 16 ; l <= XLSrange.Rows.Count-1 ; l++)
{
xlsRngMilestone = _shtOut.get_Range("B" +
l.ToString(),Type.Missing);
xlsRngPlanned = _shtOut.get_Range("C" +
l.ToString(),Type.Missing);
if(xlsRngMilestone.Value!=null)
{
if(test.Value.ToString() == xlsRngMilestone.Value.ToString() +
".PLANNED" )
{
cellloc = x;
_shtOut1.get_Range("B" + x.ToString() , Type.Missing).Value =
xlsRngPlanned.Value;
}
}
else
{
break;
}
}
}
}

_book1.Save();
_sheet1.SaveAs(@"C:\PRP
\PRPTemp.txt",Excel.XlFileFormat.xlTextWindows,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);

_sheet1 = null;
NAR(_sheet1);
ExcelApp.ActiveWindow.Close(false,@"C:\PRP\PRPTemp.xls",false);
}
catch(Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
#endregion
 
M

Mark Rae

M

Mark Rae

But how i am going to put it again in a text file in tab delimited
format

1) Fetch the data from Excel using ADO.NET into a DataSet or maybe an
OleDbDataReader

2) Instantiate a TextWriter object

3) Iterate through the rows of the data object

4) For each row, write a line to the TextWriter object

MyTextWriter.WriteLine(<RowObject>[0].ToString() + "\t" +
<RowObject>[1].ToString());
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top