当前位置:网站首页>【C#笔记】winform中保存DataGridView中的数据为Excel和CSV
【C#笔记】winform中保存DataGridView中的数据为Excel和CSV
2022-07-02 06:26:00 【寂云萧】
保存为Excel
使用system.io保存,需要导入的库:using System.IO;
//使用system.io保存
//grid为整个DataGridView对象,而filePath为要保存的路径(包括后缀名),下同
public void saveExcelIO(DataGridView grid, string filePath)
{
Thread.Sleep(1000);
StreamWriter sw = new StreamWriter(filePath, false, Encoding.GetEncoding("gb2312"));
StringBuilder sb = new StringBuilder();
//写入标题
for(int k = 0; k < grid.Columns.Count; k++)
{
if (grid.Columns[k].Visible)
{
//\t相当于是在键盘上打一个tab键
sb.Append(grid.Columns[k].HeaderText.ToString().Trim() + "\t");
}
}
//换行
sb.Append(Environment.NewLine);
//开始写入每行的数值
for (int i = 0; i < grid.Rows.Count - 1; i++)
{
System.Windows.Forms.Application.DoEvents();
for(int j = 0; j < grid.Columns.Count; j++)
{
if (grid.Columns[j].Visible)
{
//单元格有一定字节的数量限制,可能会超出,超出会出现两个单元格内容一样的情况
sb.Append(grid.Rows[i].Cells[j].Value.ToString().Trim() + "\t");
}
}
sb.Append(Environment.NewLine);//换行
}
sw.Write(sb.ToString());
sw.Flush();
sw.Close();
}
保存为CSV
//保存csv
public void saveCSV(DataGridView grid,string filePath)
{
Thread.Sleep(1000);
StreamWriter sw = new StreamWriter(filePath, false, Encoding.GetEncoding("gb2312"));
string strLine = "";
//表头
for (int i = 0; i < grid.ColumnCount; i++)
{
if (i > 0)
strLine += ",";
strLine += grid.Columns[i].HeaderText;
}
strLine.Remove(strLine.Length - 1);
sw.WriteLine(strLine);
strLine = "";
for(int j = 0; j < grid.Rows.Count; j++)
{
strLine = "";
int col = grid.Columns.Count;
for(int k = 0; k < col; k++)
{
if (k > 0 && k < col)
strLine += ",";
if (grid.Rows[j].Cells[k].Value == null)
strLine += "";
else
{
string cell = grid.Rows[j].Cells[k].Value.ToString().Trim();
//防止里面有特殊符号
cell = cell.Replace("\"", "\"\"");
cell = "\"" + cell + "\"";
strLine += cell;
}
}
sw.WriteLine(strLine);
}
sw.Close();
}
边栏推荐
- 【Sparse-to-Dense】《Sparse-to-Dense:Depth Prediction from Sparse Depth Samples and a Single Image》
- Implement interface Iterable & lt; T&gt;
- ABM thesis translation
- 【Mixed Pooling】《Mixed Pooling for Convolutional Neural Networks》
- TimeCLR: A self-supervised contrastive learning framework for univariate time series representation
- parser. parse_ Args boolean type resolves false to true
- PHP returns the corresponding key value according to the value in the two-dimensional array
- Apple added the first iPad with lightning interface to the list of retro products
- Memory model of program
- Generate random 6-bit invitation code in PHP
猜你喜欢
【FastDepth】《FastDepth:Fast Monocular Depth Estimation on Embedded Systems》
超时停靠视频生成
[Sparse to Dense] Sparse to Dense: Depth Prediction from Sparse Depth samples and a Single Image
【Mixup】《Mixup:Beyond Empirical Risk Minimization》
Faster-ILOD、maskrcnn_ Benchmark training coco data set and problem summary
What if a new window always pops up when opening a folder on a laptop
基于pytorch的YOLOv5单张图片检测实现
【MobileNet V3】《Searching for MobileNetV3》
open3d学习笔记三【采样与体素化】
深度学习分类优化实战
随机推荐
Faster-ILOD、maskrcnn_ Benchmark training coco data set and problem summary
The difference and understanding between generative model and discriminant model
Machine learning theory learning: perceptron
半监督之mixmatch
open3d学习笔记五【RGBD融合】
Win10+vs2017+denseflow compilation
【深度学习系列(八)】:Transoform原理及实战之原理篇
Semi supervised mixpatch
点云数据理解(PointNet实现第3步)
TimeCLR: A self-supervised contrastive learning framework for univariate time series representation
ABM论文翻译
Using MATLAB to realize: power method, inverse power method (origin displacement)
Huawei machine test questions
【Mixed Pooling】《Mixed Pooling for Convolutional Neural Networks》
Determine whether the version number is continuous in PHP
Calculate the difference in days, months, and years between two dates in PHP
parser. parse_ Args boolean type resolves false to true
open3d学习笔记三【采样与体素化】
Network metering - transport layer
MMDetection模型微调