C# AddIn Excel : 在带分隔符的文本文件中导出 xls 文件
C# AddIn Excel : export xls file in text file with separator
这是我的第一个 AddIn excel,我在使用分隔符(“|”)将 excel 的文件导出到文本文件时遇到了问题。
我想在同一路径中导出 excel 的文件,扩展名为“.txt”,我想用特定字符(“|”)分隔 excel 的列。
我在功能区上创建了一个按钮:
private void exportSave_Click(object sender, RibbonControlEventArgs e)
{
int lastColumns;
lastColumns = usedRange.Columns.Count;
int endCol = lastColumns;
Microsoft.Office.Interop.Excel.Worksheet xlSheet = Globals.VATTools.Application.ActiveSheet;
Microsoft.Office.Interop.Excel.Range usedRange = xlSheet.UsedRange;
// Save file in .txt in the same path
string path = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
xlSheet.SaveAs(path, Microsoft.Office.Interop.Excel.XlFileFormat.xlTextWindows);
}
我不确定格式 "xlTextWindows"。
我识别了我的列,但我不知道如何在将其导出到 .txt 之前在列之间添加特定字符
谢谢!
试试下面的代码:
public static void ExportToPipe(RibbonControlEventArgs e)
{
string ExportName = @"D:\Pipe.txt";
Excel.Window window = e.Control.Context;
Excel.Worksheet sheet = ((Excel.Worksheet)window.Application.ActiveSheet);
Excel.Range last = sheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
int lastUsedRow = last.Row;
int lastUsedColumn = last.Column;
string output = "";
for (int i = 1; i <= lastUsedRow; i++)
{
for (int j = 1; j <= lastUsedColumn; j++)
{
if (sheet.Cells[i, j].Value != null)
{
output += sheet.Cells[i, j].Value.ToString();
}
output += "|";
}
output += Environment.NewLine;
}
FileStream fs = new FileStream(ExportName, FileMode.Create, FileAccess.Write);
StreamWriter writer = new StreamWriter(fs);
writer.Write(output);
writer.Close();
}
这是我的第一个 AddIn excel,我在使用分隔符(“|”)将 excel 的文件导出到文本文件时遇到了问题。 我想在同一路径中导出 excel 的文件,扩展名为“.txt”,我想用特定字符(“|”)分隔 excel 的列。
我在功能区上创建了一个按钮:
private void exportSave_Click(object sender, RibbonControlEventArgs e)
{
int lastColumns;
lastColumns = usedRange.Columns.Count;
int endCol = lastColumns;
Microsoft.Office.Interop.Excel.Worksheet xlSheet = Globals.VATTools.Application.ActiveSheet;
Microsoft.Office.Interop.Excel.Range usedRange = xlSheet.UsedRange;
// Save file in .txt in the same path
string path = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
xlSheet.SaveAs(path, Microsoft.Office.Interop.Excel.XlFileFormat.xlTextWindows);
}
我不确定格式 "xlTextWindows"。
我识别了我的列,但我不知道如何在将其导出到 .txt 之前在列之间添加特定字符
谢谢!
试试下面的代码:
public static void ExportToPipe(RibbonControlEventArgs e)
{
string ExportName = @"D:\Pipe.txt";
Excel.Window window = e.Control.Context;
Excel.Worksheet sheet = ((Excel.Worksheet)window.Application.ActiveSheet);
Excel.Range last = sheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
int lastUsedRow = last.Row;
int lastUsedColumn = last.Column;
string output = "";
for (int i = 1; i <= lastUsedRow; i++)
{
for (int j = 1; j <= lastUsedColumn; j++)
{
if (sheet.Cells[i, j].Value != null)
{
output += sheet.Cells[i, j].Value.ToString();
}
output += "|";
}
output += Environment.NewLine;
}
FileStream fs = new FileStream(ExportName, FileMode.Create, FileAccess.Write);
StreamWriter writer = new StreamWriter(fs);
writer.Write(output);
writer.Close();
}