当前位置:网站首页>[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();
}
边栏推荐
- 基于pytorch的YOLOv5单张图片检测实现
- [CVPR‘22 Oral2] TAN: Temporal Alignment Networks for Long-term Video
- 图片数据爬取工具Image-Downloader的安装和使用
- Proof and understanding of pointnet principle
- 浅谈深度学习中的对抗样本及其生成方法
- mmdetection训练自己的数据集--CVAT标注文件导出coco格式及相关操作
- Pointnet understanding (step 4 of pointnet Implementation)
- Mmdetection trains its own data set -- export coco format of cvat annotation file and related operations
- PHP returns the abbreviation of the month according to the numerical month
- Installation and use of image data crawling tool Image Downloader
猜你喜欢
PointNet原理证明与理解
Installation and use of image data crawling tool Image Downloader
Win10+vs2017+denseflow compilation
[binocular vision] binocular stereo matching
【Hide-and-Seek】《Hide-and-Seek: A Data Augmentation Technique for Weakly-Supervised Localization xxx》
【Programming】
Correction binoculaire
[multimodal] clip model
Traditional target detection notes 1__ Viola Jones
Thesis writing tip2
随机推荐
Faster-ILOD、maskrcnn_benchmark训练自己的voc数据集及问题汇总
【Random Erasing】《Random Erasing Data Augmentation》
【C#笔记】winform中保存DataGridView中的数据为Excel和CSV
【MnasNet】《MnasNet:Platform-Aware Neural Architecture Search for Mobile》
Use Baidu network disk to upload data to the server
Open3D学习笔记一【初窥门径,文件读取】
Proof and understanding of pointnet principle
用MLP代替掉Self-Attention
Remplacer l'auto - attention par MLP
基于pytorch的YOLOv5单张图片检测实现
【Hide-and-Seek】《Hide-and-Seek: A Data Augmentation Technique for Weakly-Supervised Localization xxx》
What if the laptop task manager is gray and unavailable
超时停靠视频生成
用全连接层替代掉卷积 -- RepMLP
【DIoU】《Distance-IoU Loss:Faster and Better Learning for Bounding Box Regression》
【FastDepth】《FastDepth:Fast Monocular Depth Estimation on Embedded Systems》
【AutoAugment】《AutoAugment:Learning Augmentation Policies from Data》
【Mixed Pooling】《Mixed Pooling for Convolutional Neural Networks》
【Mixup】《Mixup:Beyond Empirical Risk Minimization》
浅谈深度学习中的对抗样本及其生成方法