当前位置:网站首页>[C # note] the data in DataGridView saved in WinForm is excel and CSV
[C # note] the data in DataGridView saved in WinForm is excel and CSV
2022-07-02 07:52:00 【Silent clouds】
Save as Excel
Use system.io preservation , Libraries that need to be imported :using System.IO;
// Use system.io preservation
//grid For the whole DataGridView object , and filePath For the path to save ( Include suffix ), The same below
public void saveExcelIO(DataGridView grid, string filePath)
{
Thread.Sleep(1000);
StreamWriter sw = new StreamWriter(filePath, false, Encoding.GetEncoding("gb2312"));
StringBuilder sb = new StringBuilder();
// Write the title
for(int k = 0; k < grid.Columns.Count; k++)
{
if (grid.Columns[k].Visible)
{
//\t It's equivalent to typing one on the keyboard tab key
sb.Append(grid.Columns[k].HeaderText.ToString().Trim() + "\t");
}
}
// Line break
sb.Append(Environment.NewLine);
// Start writing values for each line
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)
{
// Cells have a certain number of bytes , May exceed , If it exceeds the limit, the contents of two cells will be the same
sb.Append(grid.Rows[i].Cells[j].Value.ToString().Trim() + "\t");
}
}
sb.Append(Environment.NewLine);// Line break
}
sw.Write(sb.ToString());
sw.Flush();
sw.Close();
}
Save as CSV
// preservation csv
public void saveCSV(DataGridView grid,string filePath)
{
Thread.Sleep(1000);
StreamWriter sw = new StreamWriter(filePath, false, Encoding.GetEncoding("gb2312"));
string strLine = "";
// Header
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();
// Prevent special symbols inside
cell = cell.Replace("\"", "\"\"");
cell = "\"" + cell + "\"";
strLine += cell;
}
}
sw.WriteLine(strLine);
}
sw.Close();
}
边栏推荐
- 【BiSeNet】《BiSeNet:Bilateral Segmentation Network for Real-time Semantic Segmentation》
- How to turn on night mode on laptop
- Generate random 6-bit invitation code in PHP
- 【Paper Reading】
- Drawing mechanism of view (3)
- PointNet原理证明与理解
- 半监督之mixmatch
- Record of problems in the construction process of IOD and detectron2
- ModuleNotFoundError: No module named ‘pytest‘
- Deep learning classification Optimization Practice
猜你喜欢

Faster-ILOD、maskrcnn_benchmark训练coco数据集及问题汇总

The difference and understanding between generative model and discriminant model

ModuleNotFoundError: No module named ‘pytest‘

【学习笔记】Matlab自编高斯平滑器+Sobel算子求导

Semi supervised mixpatch

【Hide-and-Seek】《Hide-and-Seek: A Data Augmentation Technique for Weakly-Supervised Localization xxx》

【Mixup】《Mixup:Beyond Empirical Risk Minimization》

用全连接层替代掉卷积 -- RepMLP
![[in depth learning series (8)]: principles of transform and actual combat](/img/2e/89920de2273b6f1bc3b21a19c2ecbe.png)
[in depth learning series (8)]: principles of transform and actual combat

自然辩证辨析题整理
随机推荐
Record of problems in the construction process of IOD and detectron2
Open3D学习笔记一【初窥门径,文件读取】
TimeCLR: A self-supervised contrastive learning framework for univariate time series representation
Mmdetection trains its own data set -- export coco format of cvat annotation file and related operations
Translation of the paper "written mathematical expression recognition with bidirectionally trained transformer"
win10解决IE浏览器安装不上的问题
CPU register
基于pytorch的YOLOv5单张图片检测实现
MMDetection安装问题
open3d学习笔记五【RGBD融合】
How do vision transformer work? [interpretation of the paper]
【Cutout】《Improved Regularization of Convolutional Neural Networks with Cutout》
解决jetson nano安装onnx错误(ERROR: Failed building wheel for onnx)总结
open3d学习笔记四【表面重建】
【多模态】CLIP模型
MoCO ——Momentum Contrast for Unsupervised Visual Representation Learning
【Mixup】《Mixup:Beyond Empirical Risk Minimization》
ABM thesis translation
Mmdetection model fine tuning
【Cutout】《Improved Regularization of Convolutional Neural Networks with Cutout》