.net 如何解析csv文件并仅将指定值粘贴到Excel工作表中?
设置环境: 我正在使用vb.net开发带有.net Framework 4的Excel外接程序.net 如何解析csv文件并仅将指定值粘贴到Excel工作表中?,.net,vb.net,excel,parsing,csv,.net,Vb.net,Excel,Parsing,Csv,设置环境: 我正在使用vb.net开发带有.net Framework 4的Excel外接程序 我的目标: 使用vb.net将csv文件解析到内存中 解析后,仅将指定的值粘贴到Excel工作表中 经过研究,此链接提供了一个良好的开端: 我现在使用的代码依赖于.Net中的 我无法控制csv文件的导出方式。这就是它的样子: 这是我唯一需要的数据: 我希望有更好的方法来解决这个问题,但我不知道怎么做 有没有人能提供一个指针,说明应该使用什么样的方法?我真的很感激 附加说明: 如
我的目标:
经过研究,此链接提供了一个良好的开端:
我现在使用的代码依赖于.Net中的
我无法控制csv文件的导出方式。这就是它的样子:
这是我唯一需要的数据:
我希望有更好的方法来解决这个问题,但我不知道怎么做 有没有人能提供一个指针,说明应该使用什么样的方法?我真的很感激
附加说明: 如果要复制csv文件,请参见以下内容:
SerialNumber,312315
Model,"DD"
MessageStore,""
Version,R0V2
Run,1
RunStartTime,24-Mar-14 08:39:40
RunDuration,9 minutes 15 seconds
RunTag,"Test1"
LoggingInterval,"15.0"
LeftScaleSerialNumber,233730
LeftScaleDatatype,Upper
LeftScaleUnits,""
LeftScaleUserFactor,""
LeftScaleUserOffset,"0.00000"
LeftScaleUserResolution,""
UpperModuleSerialNumber,25
UpperModuleModel,1KSI
UpperModuleMessageStore,""
UpperModuleUserspan,""
UpperModuleUnits,""
UpperModuleUserFactor,"10"
UpperModuleUserOffset,"0.00000"
UpperModuleUserResolution,"1"
UpperModuleVersion,R090007
UpperModuleCalDue,25-Jun-13
Point#,Time,LeftScaleReading
1, 00:00:00.0,"2179"
2, 00:00:15.0,"23603"
3, 00:00:30.0,"23573"
4, 00:00:45.0,"23564"
5, 00:01:00.0,"23590"
6, 00:01:15.0,"23573"
7, 00:01:30.0,"23525"
8, 00:01:45.0,"23564"
9, 00:02:00.0,"23537"
10, 00:02:15.0,"23506"
11, 00:02:30.0,"1657"
如果输出或多或少是标准的,假设您知道您需要的行以RunStart、RunTag、RunDuration和数字开头,那么您可以通过常规流打开csv文件,读取一行(显示的输出,将其转换为字符串并检查是否包含所需的数据。如果包含,请按逗号拆分,然后根据需要输出。如果需要代码,请告诉我,我现在时间有点紧,但可以在今天晚些时候或明天创建 希望这有帮助
另外,如果您可以将CSV粘贴到此处,作为文本,我可以使用它进行测试。我想这就是我将如何进行的。如果您有将其导入excel的代码,请告诉我
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
namespace CSVParser
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//location of the file
System.IO.StreamReader _fs = new StreamReader("../../TextFile1.txt");
int _result;
while (!_fs.EndOfStream)
{
string _filestring = _fs.ReadLine();
string[] _csvstring = _filestring.Split(Convert.ToChar(","));
if(_csvstring.Length > 1)
{
//check if the string begins with run maybe ignore the first run, since you don't need it
if (_csvstring[0].StartsWith("Run") && !_csvstring[0].Equals("Run"))
{
Debug.WriteLine(_csvstring[0] + "-" + _csvstring[1]);
}
//check for the datapoints based ont heir numeric values
if(int.TryParse(_csvstring[0],out _result))
{
if (_result <= 10)
{
Debug.WriteLine(_csvstring[0] + "-" + _csvstring[1] + "-" + _csvstring[2]);
}
}
}
}
_fs.Close();
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用System.IO;
使用系统诊断;
命名空间CSVParser
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
//文件的位置
System.IO.StreamReader _fs=newstreamreader(“../../TextFile1.txt”);
int_结果;
而(!\u fs.EndOfStream)
{
字符串_filestring=_fs.ReadLine();
字符串[]\u csvstring=\u filestring.Split(Convert.ToChar(“,”);
如果(_csvstring.Length>1)
{
//检查字符串是否以run开头,可以忽略第一次运行,因为您不需要它
if(_csvstring[0].StartsWith(“Run”)和&!_csvstring[0].Equals(“Run”))
{
Debug.WriteLine(_csvstring[0]+“-”+_csvstring[1]);
}
//检查基于其数值的数据点
if(int.TryParse(_csvstring[0],out_result))
{
如果(_)结果,输出将始终是标准的。唯一不同的是数据量,但我始终只需要前10个。如果您知道如何编写此代码,我肯定很想看看。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
namespace CSVParser
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//location of the file
System.IO.StreamReader _fs = new StreamReader("../../TextFile1.txt");
int _result;
while (!_fs.EndOfStream)
{
string _filestring = _fs.ReadLine();
string[] _csvstring = _filestring.Split(Convert.ToChar(","));
if(_csvstring.Length > 1)
{
//check if the string begins with run maybe ignore the first run, since you don't need it
if (_csvstring[0].StartsWith("Run") && !_csvstring[0].Equals("Run"))
{
Debug.WriteLine(_csvstring[0] + "-" + _csvstring[1]);
}
//check for the datapoints based ont heir numeric values
if(int.TryParse(_csvstring[0],out _result))
{
if (_result <= 10)
{
Debug.WriteLine(_csvstring[0] + "-" + _csvstring[1] + "-" + _csvstring[2]);
}
}
}
}
_fs.Close();
}
}
}